2

嘿,伙计们,我已经搜索了几个小时,但我似乎找不到任何东西。我想将用户收藏的报价带入列表视图并为他们保存,但我似乎无法让它进入列表视图活动!非常感谢所有帮助!这是我的代码:

看公共课的最爱是我需要帮助的地方。

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
4

1 回答 1

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);

您必须阅读ints:

int count = extras.getInt("quotes count", 0);

UsinggetString("quotes count")将返回 null,因为没有带有该键的字符串,只有一个整数。

最后你可以像这样传递一个 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);
    }
}

当然,您需要putExtra("quotes", quotes)在第一个 Activity 中使用 Intent(我发布的)。

于 2012-07-17T23:46:16.460 回答