我的活動崩潰與空點異常,我只是不明白。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
顯示我們行show.java – rekire 2012-02-24 16:07:42
的27是我的所有 – 2012-02-24 16:09:28
代碼,但該行是第27? – 2012-02-25 13:22:20