android nullpointerexception

时间:2012-02-24 15:54:40

标签: android nullpointerexception

我的活动因空点异常而崩溃,我只是没有得到它。

public class FactsActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */

    String random;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        image();

        Button show = (Button) findViewById(R.id.button1);
        show.setOnClickListener(this);
        String[] values={"Varsity Athletes", "Bully", "Amy"};
        Arrays.sort(values);

        Typeface tf = Typeface.createFromAsset(getAssets(),
                          "fonts/JUICE_Regular.ttf");
        show.setTypeface(tf);

    }

    @Override
    public void onClick(View v) {
        Intent in=new Intent(FactsActivity.this,show.class);
        startActivity(in);
    }
    public void image()
    {

        random = String.valueOf(util_random.random(1, 4));
        String drawable = "/assets/images/img"+random+".jpg";
        LinearLayout ll = (LinearLayout)findViewById(R.id.layout);

        // load the origial BitMap
        Bitmap bitmapOrg = BitmapFactory.decodeFile(drawable);

        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        BitmapDrawable bmd = new BitmapDrawable(bitmapOrg);

        ll.setBackgroundDrawable(bmd);

        setContentView(ll);
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center|top"
    android:orientation="vertical"
    android:background="@drawable/img2">


    <LinearLayout
         android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Show facts" />

    </LinearLayout>

</LinearLayout>

logcat的:

 02-23 21:38:36.503: E/AndroidRuntime(10653): FATAL EXCEPTION: main
    02-23 21:38:36.503: E/AndroidRuntime(10653):java.lang.RuntimeException: Unable to start activity ComponentInfo{org.facts.alexander.fuchs/org.facts.alexander.fuchs.show}: java.lang.NullPointerException 
    02-23 21:38:36.503: E/AndroidRuntime(10653):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    02-23 21:38:36.503: E/AndroidRuntime(10653):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    02-23 21:38:36.503: E/AndroidRuntime(10653):    at android.app.ActivityThread.access$1500(ActivityThread.java:117) 02-23
    21:38:36.503: E/AndroidRuntime(10653):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    02-23 21:38:36.503: E/AndroidRuntime(10653):    at
android.os.Handler.dispatchMessage(Handler.java:99) 
    02-23 21:38:36.503: E/AndroidRuntime(10653):    at android.os.Looper.loop(Looper.java:130)     
    02-23 21:38:36.503: E/AndroidRuntime(10653):    at android.app.ActivityThread.main(ActivityThread.java:3691) 
    02-23 21:38:36.503: E/AndroidRuntime(10653):    at java.lang.reflect.Method.invokeNative(Native Method) 
    02-23 21:38:36.503: E/AndroidRuntime(10653):    at
java.lang.reflect.Method.invoke(Method.java:507) 
    02-23 21:38:36.503: E/AndroidRuntime(10653):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
    02-23 21:38:36.503: E/AndroidRuntime(10653):    at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
    02-23 21:38:36.503: E/AndroidRuntime(10653):    at dalvik.system.NativeStart.main(Native Method) 
    02-23 21:38:36.503: E/AndroidRuntime(10653): Caused by: java.lang.NullPointerException
    02-23 21:38:36.503: E/AndroidRuntime(10653):    at org.facts.alexander.fuchs.show.onCreate(show.java:27) 
    02-23 21:38:36.503: E/AndroidRuntime(10653):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    02-23 21:38:36.503: E/AndroidRuntime(10653):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    02-23 21:38:36.503: E/AndroidRuntime(10653):    ... 11 more

5 个答案:

答案 0 :(得分:0)

你正在设置布局两次setContentView我认为这是问题所在。

  setContentView(R.layout.main);
  setContentView(ll);

答案 1 :(得分:0)

NullPointerException发生在第27行(show.java:27)

E/AndroidRuntime(10653): at org.facts.alexander.fuchs.show.onCreate(show.java:27) 02-23 21:38:36.503:

是哪一行?

答案 2 :(得分:0)

请检查tf中的onCreate()是否为空值。

答案 3 :(得分:0)

您是否已将此活动添加到清单文件中?它可以搜索运行应用程序的主要活动,如果没有任何活动,则可能会出现此异常

答案 4 :(得分:0)

我已经解决了这个错误 我无法以这种方式导入资产 所以我改变了

String drawable = "/assets/images/img"+random+".jpg";

InputStream is = getAssets().open(drawable);