[英]Android: Logcat shows error on (.setOnClickListener)
當我運行此代碼時,它不起作用,目錄日志將我發送到該行:“ add.setOnClickListener(new View.OnClickListener(){”
這是我的代碼:
public class MainActivity extends ActionBarActivity {
int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter =0;
add = (Button) findViewById(R.id.Badd);
sub = (Button) findViewById(R.id.Bsub);
display = (TextView) findViewById(R.id.TVmain);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your Total is: " + counter);
}
});
activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.pluseonecalculator.MainActivity"
tools:ignore="MergeRootFrame" />
這是logcat:
03-27 23:14:27.150:I / Process(408):發送信號 PID:408 SIG:9 03-27 23:27:01.070:D / AndroidRuntime(420):關閉VM 03-27 23:27:01.070:W / dalvikvm(420):threadid = 1:線程退出而未捕獲(group = 0x40015560)03-27 23:27:01.090:E / AndroidRuntime(420):致命異常:主03-27 23:27:01.090:E / AndroidRuntime(420):java.lang.RuntimeException:無法啟動活動ComponentInfo {com.example.pluseonecalculator / com.example.pluseonecalculator.MainActivity}:java.lang.NullPointerException 03-27 23:27:01.090:E / AndroidRuntime(420):位於android.app.ActivityThread.performLaunchActivity(ActivityThread。 java:1647)03-27 23:27:01.090:E / AndroidRuntime(420):位於android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)03-27 23:27:01.090:E / AndroidRuntime(420) :位於android.app.ActivityThread.access $ 1500(ActivityThread.java:117)03-27 23:27:01.090:E / AndroidRuntime(420):位於android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931) 03-27 23:27:01.090:E / AndroidRuntime(420):位於android.os.Handler.disp atchMessage(Handler.java:99)03-27 23:27:01.090:E / AndroidRuntime(420):位於android.os.Looper.loop(Looper.java:123)03-27 23:27:01.090:E / AndroidRuntime(420):位於android.app.ActivityThread.main(ActivityThread.java:3683)03-27 23:27:01.090:E / AndroidRuntime(420):位於java.lang.reflect.Method.invokeNative(本機方法) 03-27 23:27:01.090:E / AndroidRuntime(420):at java.lang.reflect.Method.invoke(Method.java:507)03-27 23:27:01.090:E / AndroidRuntime(420):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)03-27 23:27:01.090:E / AndroidRuntime(420):at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:597)03-27 23:27:01.090:E / AndroidRuntime(420):在dalvik.system.NativeStart.main(本地方法)03-27 23:27:01.090:E / AndroidRuntime(420):引起創建人:java.lang.NullPointerException 03-27 23:27:01.090:E / AndroidRuntime(420):位於com.example.pluseonecalculator.MainActivity.onCreate(MainActivity.java:34)03-27 23:27:01.090:E / AndroidRuntime(420):位於android.ap p.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)03-27 23:27:01.090:E / AndroidRuntime(420):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)03-27 23:27: 01.090:E / AndroidRuntime(420):...還有11個
嘗試在此處設置偵聽器時,您的Button似乎指向null
:
add.setOnClickListener(new View.OnClickListener() {
確保您的activity_main.xml中存在一個ID為Badd
的Button,否則findViewById將返回null
:
add = (Button) findViewById(R.id.Badd);
您缺少activity_main中按鈕的xml代碼。 為了使用其ID從Java代碼實例化按鈕,必須將其添加到activity_main.xml中:
<Button
android:id="@+id/Badd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Button"/>
您在activity_main.xml
內沒有任何按鈕。 如果使用findViewById
則必須具有<Button>
(或嘗試查找的任何元素)元素作為主布局的子級(在本例中為FrameLayout
)。
即
<FrameLayout
android:stuff = "stuffystuff"
... />
<Button
android:id = "ThisIsTheIdThatWillBeFound // by findViewById(R.id.ThisIsTheIdThatWillBeFound)
.../>
<FrameLayout>
目前正在發生的事情是沒有R.id.Badd
因為android無法使用android:id = "@+id/Badd"
找到任何東西來生成它。 因此, NullPointerException
。
就您而言,將ID為Badd
和Bsub
Buttons添加到布局中,就不會有任何問題。 您的按鈕也必須具有其他屬性。 看這里
另外,如果您打算以類似的方式實現第二個按鈕,我會考慮將您的主類用作兩個按鈕的OnClickListener
(而不是創建兩個),並且在傳遞OnClick
方法的視圖時,使用其ID來控制程序通過switch (view.getId())
。 這里的例子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.