嘿,伙计们,我已经搜索了几个小时,但我似乎找不到任何东西。我想将用户收藏的报价带入列表视图并为他们保存,但我似乎无法让它进入列表视图活动!非常感谢所有帮助!这是我的代码:
看公共课的最爱是我需要帮助的地方。
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);
}
}
日志猫
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