2012-02-24 46 views
0

我的活動崩潰與空點異常,我只是不明白。android nullpointexception

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 
+0

顯示我們行show.java – rekire 2012-02-24 16:07:42

+0

的27是我的所有 – 2012-02-24 16:09:28

+0

代碼,但該行是第27? – 2012-02-25 13:22:20

回答

0

要設置佈局兩次setContentView,我認爲這就是問題所在。

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

的空指針異常在第27行(show.java:27)發生

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

哪條線是什麼?

0

請檢查tfonCreate()空值。

0

您是否將此活動添加到清單文件中?它可以搜索一個主要活動,以運行應用程序,如果沒有任何活動在那裏,你可能會得到這個異常

0

我已經解決了這個錯誤 我無法導入資產這樣 所以我改變

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

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

您可以接受此答案,以便此問題不會處於「未答覆」部分。 – Nizam 2013-10-08 14:59:25