gpt4 book ai didi

android - 在使用 Button 调试 NullPointerException 时需要帮助

转载 作者:行者123 更新时间:2023-11-29 22:16:19 25 4
gpt4 key购买 nike

我有一个带有一些按钮的关于屏幕,但我在第一个 setOnClickListener 行中不断收到 NullPointerException,这是什么问题?

关于.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();
}
}
}

关于.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)

您的帖子没有太多上下文来解释代码部分;请更清楚地解释您的情况。

最佳答案

看起来您正在将 contentView 设置为 R.layout.main,但该文件名为 about.xml。如果 main.xml 不包含 ID 为 back 的小部件,那么它将从 findViewById()

返回 null

关于android - 在使用 Button 调试 NullPointerException 时需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8387878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com