在我的问题中,我有一个小部件按钮。如果我点击它的按钮将转到下一个活动,但在我点击后我的应用程序已停止。指示是关于
NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'
低于我的代码
ArrayList<HashMap<String, String>> menuList;
String searchkey;
@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_listing);
Bundle extras = getIntent().getExtras();
searchkey = extras.getString("txtsearch").toString();
//rename actionbar title to search keyword
setTitle("Hasil Pencarian " + searchkey);
//URL with search key
menuList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
BtnDetail = (Button) findViewById(R.id.btnDetail);
BtnDetail.setOnClickListener(this);
new GetMenu().execute();
}
public void onClick (View v){
if (v.getId()==R.id.btnDetail)
{
Intent maps = new Intent(this,MapsActivity.class);
startActivity(maps);
}
}
private class GetMenu extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(Listing.this);
pDialog.setMessage("Loading,, Please Wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected Void doInBackground(Void... arg0){
HttpHandler sh = new HttpHandler();
String url_search = "http://10.0.2.2/search/search.php?txtsearch="+searchkey;
Log.e(TAG,url_search);
//Making request to url and getting response
String jsonStr = sh.makeServiceCall(url_search);
Log.e(TAG,"Response from URL :" +jsonStr);
if (jsonStr != null){
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node name
JSONArray contacts = jsonObj.getJSONArray("data");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("menu_kuliner.kd_kuliner");
String menu = c.getString("menu_kuliner.menu");
String harga = c.getString("menu_kuliner.harga");
String kuliner = c.getString("nama_kuliner.nm_kuliner");
String alamat = c.getString("nama_kuliner.alamat");
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("menu_kuliner.kd_kuliner", id);
contact.put("menu_kuliner.menu", menu);
contact.put("menu_kuliner.harga", harga);
contact.put("nama_kuliner.nm_kuliner", kuliner);
contact.put("nama_kuliner.alamat", alamat);
// adding contact to contact list
menuList.add(contact);
}
//if JSON format error
} catch (final JSONException e){
Log.e(TAG,"JSON Parsing Error :" +e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"JSON Parsing Error :" +e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.e(TAG, "Couldnt get JSON From Server !");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldnt get JSON From Server ! Check Logcat for possible Error",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
@Override
protected void onPostExecute (Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(
Listing.this, menuList, R.layout.list_item,
new String[]{"menu_kuliner.kd_kuliner", "menu_kuliner.menu", "menu_kuliner.harga", "nama_kuliner.nm_kuliner", "nama_kuliner.alamat"}, //<= ARRAY LIST
new int[]{R.id.id, R.id.menu, R.id.harga, R.id.kuliner, R.id.alamat});
lv.setAdapter(adapter);
}
}
}
这是我的Activity_listing.xml
android:id="@+id/activity_listing"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Listing" >
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
这是我的list_item.xml
<TextView android:id="@+id/menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize=" 17dip"
android:textStyle="bold" />
<TextView android:id="@+id/harga"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dip" />
<TextView android:id="@+id/kuliner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dip" />
<TextView android:id="@+id/alamat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dip" />
<Button
android:id="@+id/btnDetail"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
androidManifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBrGERGDjAf2XKeS9Q_MGd24hOX_PUTKpQ" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Listing">
</activity>
<activity android:name=".MapsActivity">
</activity>
</application>
修改
ArrayList<HashMap<String, String>> menuList;
String searchkey;
@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_listing);
Bundle extras = getIntent().getExtras();
searchkey = extras.getString("txtsearch").toString();
//rename actionbar title to search keyword
setTitle("Hasil Pencarian " + searchkey);
//URL with search key
menuList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView <?> parent, View view, int position, long id) {
if( view.getId()== R.id.btnDetail)
{
Intent maps = new Intent(Listing.this,MapsActivity.class);
startActivity(maps);
}
}
});
答案 0 :(得分:0)
您可以使用列表项单击侦听器来处理listview的单击事件
lv = (ListView) findViewById(R.id.list);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if( view.getId() == R.id.btnDetail ) {
Intent maps = new Intent(MainActivity.this, MapsActivity.class);
startActivity(maps);
}
}
});
如果您希望通过按钮单击使用上面的代码来处理单击侦听器,或者您可以删除if条件以检查按钮ID并直接从事件处理程序调用maps活动。