我有一个关于屏幕有一些按钮,但我在第一个setOnClickListener行中一直得到NullPointerException,有什么问题?
about.java
public class About extends Activity{
private Button backbutton;
private Button likebutton;
private Button faq;
private Button update;
private Button feedback;
private Button rate;
private Button help;
private String faqpath ="http://appkon.com/hdtvs/faq.html";
private String faqname ="常见问题";
private String feedbackpath="http://appkon.com/hdtvs/feedback.html" ;
private String feedbackname="反馈问题" ;
private String ratename="评价APP" ;
private String ratepath="http://appkon.com/hdtvs/rate.html" ;
private String updatepath="http://appkon.com/hdtvs/update.html" ;
private String updatename="检查更新" ;
private String wentiname ="";
private String wentipath ="";
private String gengxinname ="";
private String gengxinpath ="";
private String pingjianame ="";
private String pingjiapath ="";
private String changjianpath ="";
private String changjianname ="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
backbutton=(Button) findViewById(R.id.back);
likebutton=(Button) findViewById(R.id.share);
faq =(Button)findViewById(R.id.faq);
update =(Button)findViewById(R.id.update);
feedback =(Button)findViewById(R.id.feedback);
rate =(Button)findViewById(R.id.rate);
help =(Button)findViewById(R.id.child);
try{
backbutton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(About.this, HDtvs.class);
startActivity(intent);
About.this.finish();
}
});
likebutton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(About.this, Renrenframe.class);
startActivity(intent);
About.this.finish();
}
});
faq.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(About.this, Aboutframe.class);
Bundle bundle = new Bundle();
bundle.putString("changjianpath",faqpath);
bundle.putString("changjianname",faqname);
intent.putExtras(bundle);
startActivity(intent);
About.this.finish();
}
});
feedback.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(About.this, Aboutframe.class);
Bundle bundle = new Bundle();
bundle.putString("wentipath",feedbackpath);
bundle.putString("wentiname",feedbackname);
intent.putExtras(bundle);
startActivity(intent);
About.this.finish();
}
});
rate.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(About.this, Aboutframe.class);
Bundle bundle = new Bundle();
bundle.putString("pingjiapath",ratepath);
bundle.putString("pingjianame",ratename);
intent.putExtras(bundle);
startActivity(intent);
About.this.finish();
}
});
update.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(About.this, Aboutframe.class);
Bundle bundle = new Bundle();
bundle.putString("gengxinpath",updatepath);
bundle.putString("gengxinname",updatename);
intent.putExtras(bundle);
startActivity(intent);
About.this.finish();
}
});
help.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(About.this, WapsList.class);
startActivity(intent);
About.this.finish();
}
});
}catch (Exception e)
{
e.printStackTrace();
}
}
}
about.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center|top"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_background_repeat"
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="45dip"
android:id="@+id/header"
android:background="@drawable/header"
>
<Button android:id="@+id/back"
android:textSize="14.0sp"
android:textColor="#ffe1ebef"
android:gravity="center"
android:background="@drawable/button_back"
android:layout_width="54.0dip"
android:layout_height="33.0dip"
android:layout_marginLeft="10.0dip"
android:text="@string/back"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
android:textSize="18.0sp"
android:layout_marginTop="8dip"
android:text="@string/about"/>
<Button android:id="@+id/share"
android:textSize="14.0sp"
android:textColor="#ffe1ebef"
android:gravity="center"
android:background="@drawable/share"
android:layout_width="54.0dip"
android:layout_height="33.0dip"
android:layout_marginRight="10.0dip"
android:text="@string/share"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dip"/>
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/function"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center|top"
android:layout_marginTop="20dip">
<TextView android:id="@+id/funcdesc"
android:layout_width="280dip"
android:layout_height="wrap_content"
android:text="@string/funcdesc"
android:gravity="center" />
<Button android:id="@+id/feedback"
android:textColor="#6f789b"
android:textSize="16.0sp"
android:gravity="center"
android:background="@drawable/button"
android:layout_width="185.0dip"
android:layout_height="44.0dip"
android:layout_marginRight="10.0dip"
android:text="@string/feedback"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dip"/>
<Button android:id="@+id/rate"
android:textColor="#6f789b"
android:textSize="16.0sp"
android:gravity="center"
android:background="@drawable/button"
android:layout_width="185.0dip"
android:layout_height="44.0dip"
android:layout_marginRight="10.0dip"
android:text="@string/rate"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dip"/>
<Button android:id="@+id/faq"
android:textColor="#6f789b"
android:textSize="16.0sp"
android:gravity="center"
android:background="@drawable/button"
android:layout_width="185.0dip"
android:layout_height="44.0dip"
android:layout_marginRight="10.0dip"
android:text="@string/faq"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dip"/>
<Button android:id="@+id/update"
android:textColor="#6f789b"
android:textSize="16.0sp"
android:gravity="center"
android:background="@drawable/button"
android:layout_width="185.0dip"
android:layout_height="44.0dip"
android:layout_marginRight="10.0dip"
android:text="@string/update"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dip" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/donation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center|top"
android:layout_marginTop="20dip">
<TextView android:id="@+id/funcdesc"
android:layout_width="280dip"
android:layout_height="wrap_content"
android:text="@string/helpdesc"
android:gravity="center" />
<Button android:id="@+id/child"
android:textColor="#6f789b"
android:textSize="16.0sp"
android:gravity="center"
android:background="@drawable/button"
android:layout_width="185.0dip"
android:layout_height="44.0dip"
android:layout_marginRight="10.0dip"
android:text="@string/child"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dip" />
</LinearLayout>
</LinearLayout>
这是我的错误追踪
12-05 13:36:27.466: WARN/System.err(308): java.lang.NullPointerException
12-05 13:36:27.476: WARN/System.err(308): at com.appkon.hdtvs.About.onCreate(About.java:59)
12-05 13:36:27.486: WARN/System.err(308): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-05 13:36:27.506: WARN/System.err(308): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
12-05 13:36:27.506: WARN/System.err(308): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-05 13:36:27.526: WARN/System.err(308): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-05 13:36:27.537: WARN/System.err(308): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-05 13:36:27.546: WARN/System.err(308): at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 13:36:27.556: WARN/System.err(308): at android.os.Looper.loop(Looper.java:123)
12-05 13:36:27.576: WARN/System.err(308): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-05 13:36:27.586: WARN/System.err(308): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 13:36:27.596: WARN/System.err(308): at java.lang.reflect.Method.invoke(Method.java:521)
12-05 13:36:27.596: WARN/System.err(308): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-05 13:36:27.607: WARN/System.err(308): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-05 13:36:27.616: WARN/System.err(308): at dalvik.system.NativeStart.main(Native Method)
您的帖子没有太多上下文来解释代码部分;请更清楚地解释一下你的情景。
答案 0 :(得分:1)
您似乎将contentView设置为R.layout.main
,但该文件名为about.xml
。如果main.xml
未包含标识为back
的窗口小部件,则它将从findViewById()
返回null
答案 1 :(得分:1)
您的ID {1}}按钮位于R.id.back
,而您充气R.layout.main
请尝试使用about.xml
。