Android的 - 无法找到在活动课android.app.application方法playaudio(视图) [英] Android - Could not find a method playaudio(View) in the activity class android.app.application
问题描述
我阻塞小时了,我想使用XML onclick属性来处理列表视图内按一下按钮。
I am blocked for hours now, I am trying to handle a button click inside a listview using the xml onClick attribute.
下面是我为result.xml:
Here is my result.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TextView
android:id="@+id/url"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone"/>
<TextView
android:id="@+id/title"
style="@android:style/TextAppearance.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/play"
android:singleLine="true"
android:text="title"
android:textColor="#000000" />
<TextView
android:id="@+id/artist"
style="@android:style/TextAppearance.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/title"
android:layout_toLeftOf="@+id/play"
android:singleLine="true"
android:text="artist"
android:textColor="#7D7D7D" />
<TextView
android:id="@+id/duration"
style="@android:style/TextAppearance.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/artist"
android:layout_alignParentBottom="true"
android:layout_below="@id/artist"
android:layout_toLeftOf="@+id/play"
android:singleLine="true"
android:text="03:24"
android:textColor="#7D7D7D" />
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toLeftOf="@+id/download"
android:src="@drawable/play"
android:onClick="playAudio" />
<ImageButton
android:id="@+id/download"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:src="@drawable/download" />
</RelativeLayout>
这是我的search.java文件
here is my search.java file
package com.example.testxxx;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class Search extends ListActivity {
private TextView mTextView;
private ListView mListView;
private String query;
private String queryencoded;
private static Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
mTextView = (TextView) findViewById(R.id.text);
mListView = (ListView) findViewById(android.R.id.list);
mContext = getApplicationContext();
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
query = intent.getStringExtra(SearchManager.QUERY);
try {
queryencoded = URLEncoder.encode(query, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.w("myApp", "aaaaa");
Toast.makeText(Search.this, (query), Toast.LENGTH_LONG).show();
mTextView.setText("testing connexion");
if(isConnected()){
mTextView.setText("Loading...");
new HttpAsyncTask().execute("url to json");
}
else{
mTextView.setBackgroundColor(0xffcc0000);
mTextView.setText("You are NOT connected");
}
}
}
public boolean isConnected(){
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
public static String GET(String url){
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return GET(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
try {
JSONObject jsonobject = new JSONObject(result);
mTextView.setText((String) jsonobject.get("count"));
Integer count = Integer.parseInt((String) jsonobject.get("count"));
if (count == 0) {
// There are no results
mTextView.setText(getString(R.string.no_results, query));
} else {
// Display the number of results
String countString = getResources().getQuantityString(R.plurals.search_results,
count, new Object[] {count, query});
mTextView.setText(countString);
// Specify the columns we want to display in the result
String[] from = new String[] { "title",
"artist",
"duration",
"url"};
// Specify the corresponding layout elements where we want the columns to go
int[] to = new int[] { R.id.title,
R.id.artist,
R.id.duration,
R.id.url};
// -- container for all of our list items
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
// -- list item hash re-used
Map<String, String> group;
//create audio json array
JSONArray audios = jsonobject.getJSONArray("audio");
for (int i = 0; i < audios.length(); i++) {
JSONObject audio = audios.getJSONObject(i);
//get info
String title = audio.getString("title");
String artist = audio.getString("artist");
String duration = audio.getString("duration");
String durationformated = getDurationString(Integer.parseInt(duration));
String url = audio.getString("url");
// -- create record
group = new HashMap<String, String>();
group.put( "title", title );
group.put( "artist", artist );
group.put( "duration", durationformated );
group.put( "url", url );
groupData.add(group);
}
SimpleAdapter adapter = new SimpleAdapter(mContext , groupData, R.layout.result,
from,
to );
setListAdapter( adapter );
}
}
catch (JSONException e)
{
// TODO Auto-generated catch block
Log.w("myApp", e);
}
}
public void playAudio(View v) {
//get the row the clicked button is in
LinearLayout vwParentRow = (LinearLayout)v.getParent();
TextView urlview = (TextView)vwParentRow.findViewById(R.id.url);
String url = (String) urlview.getText();
Toast.makeText(mContext, (url), Toast.LENGTH_LONG).show();
}
}
private String getDurationString(int seconds) {
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
seconds = seconds % 60;
return twoDigitString(minutes) + ":" + twoDigitString(seconds);
}
private String twoDigitString(int number) {
if (number == 0) {
return "00";
}
if (number / 10 == 0) {
return "0" + number;
}
return String.valueOf(number);
}
}
这是错误,我得到
02-10 20:00:31.995: E/AndroidRuntime(19483): java.lang.IllegalStateException: Could not find a method playAudio(View) in the activity class android.app.Application for onClick handler on view class android.widget.ImageButton with id 'play'
我试图放置playAudio方法搜索类中,里面的HttpAsyncTask和MainActivity类别里面,没有什么工作我仍然有同样的错误。
I have tried to place the playAudio method inside the Search class, inside the HttpAsyncTask and inside the MainActivity class, nothing is working I still have the same error.
我也想知道为什么是错误引用到android.app.application类布局从搜索> HttpAsyncTask
I'm also wondering why is the error referencing to the android.app.application class as the layout is from Search > HttpAsyncTask
也许这就是错误的原因,但后来我还能有什么办法解决?
Maybe this is the cause of the error, but then what could I do to fix it?
感谢您了:)
推荐答案
在这里
mContext = getApplicationContext();
意味着你使用的是应用
为上下文
(通常不是一个好主意。)
Means you are using the Application
as a Context
(usually not a very good idea.)
下面
SimpleAdapter adapter = new SimpleAdapter(mContext , groupData, R.layout.result
您正在使用这种适配器应用程序上下文,这意味着这种情况下是用来充气为result.xml
布局。
You are using that Application context in this adapter, meaning this context is used to inflate the result.xml
layout.
其结果是,对于一个 playAudio
在应用程序<在
的onClick
参数搜索/ code>类。
The consequence is that the onClick
parameter searches for a playAudio
in the Application
class.
最后,公共无效playAudio(视图v){
是不是在你的活动,它是在你的的AsyncTask
Finally, public void playAudio(View v) {
is not in your activity, it is in your AsyncTask
要解决这个问题,
-
mContext =这一点;
另外,不要让它静态 - 将
playAudio
在活动
- 瞧!
mContext = this;
Also, don't make it static- move
playAudio
in theActivity
- Voilà!
这篇关于Android的 - 无法找到在活动课android.app.application方法playaudio(视图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!