我从我的解析对象获取数据,数据显示在各种视图中。
但是,只要我单击showMenu按钮(onClick方法为showMenu),我的应用程序就会崩溃
这是我的代码
public class SingleRestraunt extends ActionBarActivity {
private GoogleMap map;
TextView resteName, resteCuisine, resteLocation, resteAddress, restePrice,
resteTimings, restePayment, resteRating, resteWebsite;
String restName, obj, restCuisine, restLocation, restAddress, restPrice,
restTimings, restPayment, restRating, restWebsite;
String[] menu = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_restraunt);
resteName = (TextView) findViewById(R.id.restrauntName);
resteCuisine = (TextView) findViewById(R.id.restrauntCuisine);
resteLocation = (TextView) findViewById(R.id.restrauntLocation);
resteAddress = (TextView) findViewById(R.id.restrauntAddress);
restePrice = (TextView) findViewById(R.id.restrauntPrice);
resteTimings = (TextView) findViewById(R.id.restrauntTimings);
restePayment = (TextView) findViewById(R.id.restrauntPayment);
resteRating = (TextView) findViewById(R.id.restrauntRating);
resteWebsite = (TextView) findViewById(R.id.restrauntWebsite);
Intent i = getIntent();
obj = i.getStringExtra("restId");
getDetails(obj);
}
private void getDetails(String obj) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("resdb");
query.getInBackground(obj, new GetCallback<ParseObject>() {
@Override
public void done(ParseObject object, ParseException e) {
if (e == null) {
restName = object.getString("name");
restCuisine = object.getString("cuisine");
restLocation = object.getString("locality");
restAddress = object.getString("address");
restPrice = object.getString("price");
restTimings = object.getString("timings");
restPayment = object.getString("accepted");
restRating = object.getString("ratings");
restWebsite = object.getString("URL");
JSONArray test = object.getJSONArray("menu");
for (int i = 0; i < test.length(); i++) {
try {
String menu1 = (String) test.get(i);
if (menu1 == null)
menu[i] = menu1;
} catch (JSONException e1) {
e1.printStackTrace();
}
}
addData();
} else {
e.printStackTrace();
}
}
});
}
public void addData() {
resteName.setText(restName);
resteCuisine.setText(restCuisine);
resteLocation.setText(restLocation);
resteAddress.setText(restAddress);
restePrice.setText(restPrice);
resteTimings.setText(restTimings);
restePayment.setText(restPayment);
resteRating.setText(restRating);
resteWebsite.setText(restWebsite);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.single_restraunt, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void Review(View v) {
Intent i = new Intent(SingleRestraunt.this, ReviewPage.class);
i.putExtra("RestName", restName);
startActivity(i);
}
public void Offer(View v) {
Intent i = new Intent(SingleRestraunt.this, OfferPage.class);
i.putExtra("RestName", restName);
startActivity(i);
}
public void showMenu(View v) {
Bundle b = new Bundle();
b.putStringArray("menuList", menu);
Intent i = new Intent(SingleRestraunt.this, MenuActivity.class);
i.putExtras(b);
startActivity(i);
}
public void Share(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = ("Hey! I visited "
+ restName
+ " recommended to me by the Gastronoma App! Visit their site here " + restWebsite);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
}
仅当我单击showMenu按钮时,该应用程序似乎崩溃。到那时为止,该应用程序可以正常运行。所有其他按钮也可以完美地完成其预期的任务。应用程序崩溃时显示的错误是
09-02 09:54:22.307: E/AndroidRuntime(3278): Process: com.example.gastronomaapp, PID: 3278
09-02 09:54:22.307: E/AndroidRuntime(3278): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.gastronomaapp/com.example.gastronomaapp.MenuActivity}: java.lang.NullPointerException
打开任何建议。按要求编辑我已添加了MenuActivity代码和清单文件
public class MenuActivity extends Activity {
WebView web = (WebView) findViewById(R.id.webView1);
TextView next = (TextView) findViewById(R.id.next);
TextView prev = (TextView) findViewById(R.id.prev);
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Bundle b = this.getIntent().getExtras();
String[] array = b.getStringArray("menuList");
getMenu(array);
}
public void getMenu(final String[] array) {
String menu = array[i];
web.loadUrl(menu);
visibility(i, array);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i++;
String menu = array[i];
web.loadUrl(menu);
visibility(i, array);
}
});
prev.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i--;
String menu = array[i];
web.loadUrl(menu);
visibility(i, array);
}
});
}
private void visibility(int i, String[] array) {
if (i == 0)
prev.setVisibility(View.GONE);
if (i == array.length - 1)
next.setVisibility(View.GONE);
}
和清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gastronomaapp"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.example.gastronomaapp.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="com.example.gastronomaapp.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<!--
<application>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
-->
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ResterauntList"
android:label="@string/title_activity_resteraunt_list" >
</activity>
<activity
android:name=".Map"
android:label="@string/title_activity_map" >
</activity>
<activity
android:name=".Button1List"
android:label="@string/title_activity_button1_list" >
</activity>
<activity
android:name=".SingleRestraunt"
android:label="@string/title_activity_single_restraunt" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAaC-32WynuOO6stpRaK6LSxRMwCuvgdE4" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".ReviewPage"
android:label="@string/title_activity_review_page" >
</activity>
<activity
android:name=".OfferPage"
android:label="@string/title_activity_offer_page" >
</activity>
<activity
android:name=".ImageMenu"
android:label="@string/title_activity_image_menu" >
</activity>
<activity
android:name=".MenuActivity"
android:label="@string/title_activity_menu" >
</activity>
</application>
</manifest>
最佳答案
您必须执行以下操作:
Intent i = new Intent("Activity")
i.putExtra(key, ArrayName)
Intent i = getIntent();
String[] array = i.getExtras().getString(keyname);
就是这样。