[英]Android- Null pointer exception repeated crashing
I am beginner here so solution maybe trivial. 我是这里的初学者,因此解决方案可能微不足道。 I'm just trying to learn and the app is crashing with a null pointer exception
but I can't see where that could be. 我只是在尝试学习,应用程序因null pointer exception
而崩溃,但我看不到可能在哪里。 Here's the logcat: 这是logcat:
01-31 05:36:30.434: D/AndroidRuntime(1824): Shutting down VM
01-31 05:36:30.434: W/dalvikvm(1824): threadid=1: thread exiting with uncaught exception (group=0xb3aceb90)
01-31 05:36:30.454: E/AndroidRuntime(1824): FATAL EXCEPTION: main
01-31 05:36:30.454: E/AndroidRuntime(1824): Process: com.thecloset, PID: 1824
01-31 05:36:30.454: E/AndroidRuntime(1824): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.thecloset/com.thecloset.MainActivity}: java.lang.NullPointerException
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.os.Handler.dispatchMessage(Handler.java:102)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.os.Looper.loop(Looper.java:137)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.ActivityThread.main(ActivityThread.java:4998)
01-31 05:36:30.454: E/AndroidRuntime(1824): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 05:36:30.454: E/AndroidRuntime(1824): at java.lang.reflect.Method.invoke(Method.java:515)
01-31 05:36:30.454: E/AndroidRuntime(1824): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-31 05:36:30.454: E/AndroidRuntime(1824): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-31 05:36:30.454: E/AndroidRuntime(1824): at dalvik.system.NativeStart.main(Native Method)
01-31 05:36:30.454: E/AndroidRuntime(1824): HERE!!!!!!! Caused by: java.lang.NullPointerException HERE!!!!!
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.Activity.findViewById(Activity.java:1883)
01-31 05:36:30.454: E/AndroidRuntime(1824): at com.thecloset.MainActivity.<init>(MainActivity.java:16)
01-31 05:36:30.454: E/AndroidRuntime(1824): at java.lang.Class.newInstanceImpl(Native Method)
01-31 05:36:30.454: E/AndroidRuntime(1824): at java.lang.Class.newInstance(Class.java:1208)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
01-31 05:36:30.454: E/AndroidRuntime(1824): ... 11 more
Heres the xml: 继承人的XML:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="-5dp"
android:layout_y="65dp"
android:rotation="90"
android:text="secondimage" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="53dp"
android:layout_y="0dp"
android:src="@drawable/im1" />
</AbsoluteLayout>
And the Java: 和Java:
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements View.OnClickListener {
Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thecloset);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
img.setImageResource(R.drawable.image2);
}
});
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
I followed lots of advice but no luck. 我听了很多建议,但没有运气。
move following code after setContentView
in onCreate() 在onCreate()中的setContentView
之后移动以下代码
Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
so , your class must be like, 因此,您的课程必须像
public class MainActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thecloset);
Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
img.setImageResource(R.drawable.image2);
}
});
You are searching for an id in the layout before the layout is inflated (as what is not inside a method is done during class initialization), move the findViewById calls to your onCreate method : 您要在版式膨胀之前在版式中搜索ID(因为在类初始化期间完成了方法内部的操作),请将findViewById调用移至onCreate方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thecloset);
Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
// the rest of your code
}
you can't find id of UI element before onCreate() method...So you must be find it on onCreate() method. 您无法在onCreate()方法之前找到UI元素的ID ...因此必须在onCreate()方法中找到它。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thecloset);
Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
img.setImageResource(R.drawable.image2);
}
});
Move these lines: 移动这些行:
Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
to after your setContentView() 到setContentView()之后
You're asking the Activity to find the views before it's loaded them 您要让Activity在加载视图之前先找到它们
Change 更改
Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thecloset);
To 至
Button button; = (Button) findViewById(R.id.button1);
ImageView img; = (ImageView) findViewById(R.id.image);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thecloset);
button = (Button) findViewById(R.id.button1);
img = (ImageView) findViewById(R.id.image);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.