0

该应用程序有两个活动,PlanetListViewActivity.javaMainActivity.java。问题似乎就在线:

int pos = b.getInt("com.biekerwebdesign.scrollingplanets.Position=", "-"+pos);

当该行被注释掉时,应用程序打开并且不 FC。

package com.biekerwebdesign.scrollingplanets;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView) findViewById(R.id.textView1);
        Bundle b = getIntent().getExtras();
        int pos = b.getInt("com.biekerwebdesign.scrollingplanets.Position"); 
        Log.i("com.biekerwebdesign.scrollingplanets.Position=", "-"+pos);

        if(pos==0) {
            tv.setTextSize(20);
            tv.setText("MERCURY \n Aphelion \n69,816,900 km\n0.466 697 AU\nPerihelion\n 46,001,200 km\n");
        }

        if(pos==2) {
            tv.setText("earth");
        } 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

这是PlanetListViewActivity.java

package com.biekerwebdesign.scrollingplanets;

import java.util.ArrayList;
import java.util.Arrays;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.app.Activity;
import android.content.Intent;
//import android.util.Log;

public class PlanetListViewActivity extends Activity {
    private ListView mainListView ;
    private ArrayAdapter<String> listAdapter ;

    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Find the ListView resource. 
        mainListView = (ListView) findViewById( R.id.mainListView );

        // Create and populate a List of planet names.
        String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
                                      "Jupiter", "Saturn", "Uranus", "Neptune",
                                          "Alpha Centari A"};  

        ArrayList<String> planetList = new ArrayList<String>();
        planetList.addAll( Arrays.asList(planets) );
        //my change 

        // Create ArrayAdapter using the planet list.
        listAdapter = new ArrayAdapter<String>(this, R.layout.planetrow, planetList);

        // Add more planets. If you passed a String[] instead of a List<String> 
        // into the ArrayAdapter constructor, you must not add more items. 
        // Otherwise an exception will occur.
        listAdapter.add( "Ceres" );
        listAdapter.add( "Pluto" );
        listAdapter.add( "Haumea" );
        listAdapter.add( "Makemake" );
        listAdapter.add( "Eris" );
        listAdapter.remove( "Alpha Centari A");

        // Set the ArrayAdapter as the ListView's adapter.
        mainListView.setAdapter( listAdapter );

        mainListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                long arg3) {

                //Log.i("m", "-"+pos);

                Intent myIntent = new Intent(PlanetListViewActivity.this,
                                             MainActivity.class);
                myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                myIntent.putExtra("com.biekerwebdesign.scrollingplanets.Position", pos);
                startActivity(myIntent);
            }
        });
    }
}

这是我的Logcat:

10-10 08:43:03.323: D/AndroidRuntime(29285): Shutting down VM
10-10 08:43:03.323: W/dalvikvm(29285): threadid=1: thread exiting with uncaught exception (group=0x418a7700)
10-10 08:43:03.328: E/AndroidRuntime(29285): FATAL EXCEPTION: main
10-10 08:43:03.328: E/AndroidRuntime(29285): java.lang.RuntimeException: Unable to start activity   ComponentInfo{com.biekerwebdesign.scrollingplanets/com.biekerwebdesign.scrollingplanets.MainActivity}: java.lang.NullPointerException
10-10 08:43:03.328: E/AndroidRuntime(29285):    at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at android.os.Looper.loop(Looper.java:137)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at android.app.ActivityThread.main(ActivityThread.java:5289)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at java.lang.reflect.Method.invokeNative(Native Method)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at java.lang.reflect.Method.invoke(Method.java:525)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at dalvik.system.NativeStart.main(Native Method)
10-10 08:43:03.328: E/AndroidRuntime(29285): Caused by: java.lang.NullPointerException
10-10 08:43:03.328: E/AndroidRuntime(29285):    at com.biekerwebdesign.scrollingplanets.MainActivity.onCreate(MainActivity.java:17)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at android.app.Activity.performCreate(Activity.java:5133)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-10 08:43:03.328: E/AndroidRuntime(29285):    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
10-10 08:43:03.328: E/AndroidRuntime(29285):    ... 12 more

所以基本上,当我不尝试在OnCreate()方法中获取 int pos 时,应用程序会打开但不显示任何内容。但是当我尝试获取 int pos 时,应用程序 FC 在启动时。MainActivity如果没有应用程序 FC ,我将如何获得 int pos ?

4

1 回答 1

0

您在第 17 行有一个NullPointerExceptionin MainActivity,因为getIntent().getExtras()可能为空。

您必须首先检查是否b != null.

这意味着:

int pos;
if (b == null) {
    pos = 0;
}
else {
    pos = b.getInt("com.biekerwebdesign.scrollingplanets.Position");
}

您发布的 LogCat 的堆栈跟踪在这种情况下非常有用。您应该始终寻找Caused by: ...线条。

于 2013-10-10T15:51:40.180 回答