看公共课最喜欢的是我需要帮助的地方。
public class Startme extends Activity {
Random gen= new Random();
int num = 0;
String[] quotes={
"End of Quotes, hit next",
"QUOTE2",
"QUOTE3",
"QUOTE4",
"QUOTE5",};
public void favorite(View F) {
Intent intent = new Intent(Startme.this, Fav.class);
intent.putExtra("quotes",num);
}
public void next(View N){
num++;
if(num == 5)
num = 0;
TextView text = (TextView) findViewById(R.id.Quote);
text.setText( quotes[num]);
TextView number = (TextView) findViewById(R.id.Qn);
number.setText(""+num);
}
public void back(View B){
if(num==0)
num=4;
else
num--;
TextView text = (TextView) findViewById(R.id.Quote);
text.setText(quotes[num]);
TextView number = (TextView) findViewById(R.id.Qn);
number.setText(""+num);
}
public void rand(View G) {
num = gen.nextInt(5);
TextView text = (TextView) findViewById(R.id.Quote);
text.setText(quotes[num]);
TextView number = (TextView) findViewById(R.id.Qn);
number.setText(""+num);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.startquotes);
TextView Dark=(TextView)findViewById(R.id.Quote);
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/font1.ttf");
Dark.setTypeface(face);
num = gen.nextInt(5);
TextView text = (TextView) findViewById(R.id.Quote);
text.setText( quotes[num]);
TextView number = (TextView) findViewById(R.id.Qn);
number.setText(""+num);
}
public void favs(View F){
Intent intent = new Intent(this, Fav.class);
startActivity(intent);
}
}
这是我用意图
制作的列表视图类public class Fav extends Activity {
public ArrayAdapter<String> lvAdapter;
public ListView lv;
String extras2;
String[] datos = new String[] {extras2};
@Override
public void onCreate(Bundle savedInstanceState) {
super.setContentView(R.layout.faves);
Bundle extras = getIntent().getExtras();
extras2 = extras.getString("quotes");
lvAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,datos);
lv = (ListView) findViewById(R.id.list);
lv.setAdapter(lvAdapter);
}
}
logcat的
07-18 04:10:30.565: E/AndroidRuntime(7370): Uncaught handler: thread main exiting due to uncaught exception
07-18 04:10:30.597: E/AndroidRuntime(7370): java.lang.RuntimeException: Unable to start activity ComponentInfo{me.me.quote/me.me.quote.Fav}: java.lang.NullPointerException
07-18 04:10:30.597: E/AndroidRuntime(7370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
07-18 04:10:30.597: E/AndroidRuntime(7370): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
07-18 04:10:30.597: E/AndroidRuntime(7370): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
07-18 04:10:30.597: E/AndroidRuntime(7370): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
07-18 04:10:30.597: E/AndroidRuntime(7370): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 04:10:30.597: E/AndroidRuntime(7370): at android.os.Looper.loop(Looper.java:123)
07-18 04:10:30.597: E/AndroidRuntime(7370): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-18 04:10:30.597: E/AndroidRuntime(7370): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 04:10:30.597: E/AndroidRuntime(7370): at java.lang.reflect.Method.invoke(Method.java:521)
07-18 04:10:30.597: E/AndroidRuntime(7370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-18 04:10:30.597: E/AndroidRuntime(7370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-18 04:10:30.597: E/AndroidRuntime(7370): at dalvik.system.NativeStart.main(Native Method)
07-18 04:10:30.597: E/AndroidRuntime(7370): Caused by: java.lang.NullPointerException
07-18 04:10:30.597: E/AndroidRuntime(7370): at me.me.quote.Fav.onCreate(Fav.java:34)
07-18 04:10:30.597: E/AndroidRuntime(7370): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-18 04:10:30.597: E/AndroidRuntime(7370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
07-18 04:10:30.597: E/AndroidRuntime(7370): ... 11 more
答案 0 :(得分:3)
我猜你在使用fav()时会崩溃,因为你没有传递任何额外内容来接收:
Bundle extras = getIntent().getExtras();
extras2 = extras.getString("quotes");
检查是否为空,因此您不会获得空指针异常:
Bundle extras = getIntent().getExtras();
if(extras != null)
extras2 = extras.getString("quotes");
num
是int
,不是字符串:
intent.putExtra("quotes count", num);
您必须阅读int
s:
int count = extras.getInt("quotes count", 0);
使用getString("quotes count")
将返回null,因为没有带该键的String,只有整数。
终于你可以像这样传递String []:
intent.putExtra("quotes", quotes);
用这个阅读:
String[] quotes = extras.getStringArray("quotes");
希望有所帮助!
<强>加成强>
您的String数组datos
有一个空条目,当您更改extras2
时它不会自动更新,重写Fav就像这样:
public class Fav extends Activity {
public ArrayAdapter<String> lvAdapter;
public ListView lv;
String[] datos;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.faves);
// Check if extras is null, depending on implementation may need to check if Intent is null too
Bundle extras = getIntent().getExtras();
if(extras != null)
datos = extras.getStringArray("quotes");
else {
Log.w("Fav", "No string array passed, ListView will be empty!");
datos = new String[] {""};
}
lvAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datos);
lv = (ListView) findViewById(R.id.list);
lv.setAdapter(lvAdapter);
}
}
当然你需要在第一个Activity中使用putExtra("quotes", quotes)
Intent(我发布的)。