setOnClickListener 使我的应用程序崩溃 [重复]

Posted

技术标签:

【中文标题】setOnClickListener 使我的应用程序崩溃 [重复]【英文标题】:setOnClickListener makes my app crash [duplicate] 【发布时间】:2014-07-07 19:45:12 【问题描述】:

我知道这个问题已被问过很多次,但其他答案都没有帮助我。当我注释掉 setOnClickListener 时,我的代码运行良好。

所以,这是我的主要活动......

public class StartingPoint extends ActionBarActivity 

int ctr = 0;
Button add, sub;
TextView display;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_starting_point);

    if (savedInstanceState == null) 
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    

    //find views
    display = (TextView) findViewById(R.id.tvTotal);
    add = (Button)this. findViewById(R.id.bAdd);
    sub = (Button)this. findViewById(R.id.bSub);

    //click listeners
    OnClickListener lis = new OnClickListener() 
        @Override
        public void onClick(View v) 
            // TODO Auto-generated method stub
            ctr++;
            display.setText("The total is " + ctr);
        
    ;
    add.setOnClickListener(lis);

    /**sub.setOnClickListener(new View.OnClickListener() 

        @Override
        public void onClick(View v) 
            // TODO Auto-generated method stub
            ctr--;
            display.setText("The total is " + ctr);
        
    );*/


@Override
public boolean onCreateOptionsMenu(Menu menu) ...

@Override
public boolean onOptionsItemSelected(MenuItem item) ...

public static class PlaceholderFragment extends Fragment 

    public PlaceholderFragment() 
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
        View rootView = inflater.inflate(R.layout.fragment_starting_point,
                container, false);
        return rootView;
    

add.setOnClickListener(lis);导致应用崩溃。

这是fragment_starting_point.xml...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="vertical" >

<TextView
    android:id="@+id/tvTotal"
    android:layout_
    android:layout_
    android:text="The total is 0"
    android:layout_gravity="top"
    android:gravity="center"
    android:textSize="30sp"
    android:textColor="@color/red"
/>

<Button
    android:id="@+id/bAdd"
    android:layout_
    android:layout_
    android:text="Add 1"
    android:textSize="20sp"
    android:layout_gravity="center"
    android:gravity="center"
    />

<Button
    android:id="@+id/bSub"
    android:layout_
    android:layout_
    android:text="Subtract 1"
    android:textSize="20sp"
    android:layout_gravity="center"
    android:gravity="center"
/>

logCat 是这么说的……

05-19 23:23:09.700: D/CLIPBOARD(16417): Hide Clipboard dialog at Starting input:         finished by someone else... !
05-19 23:23:34.085: I/dalvikvm(16518): Could not find method     android.widget.LinearLayout$LayoutParams.<init>, referenced from method     android.support.v7.internal.view.menu.ActionMenuView$LayoutParams.<init>
05-19 23:23:34.085: W/dalvikvm(16518): VFY: unable to resolve direct method 8208:    Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
05-19 23:23:34.085: D/dalvikvm(16518): VFY: replacing opcode 0x70 at 0x0000
05-19 23:23:34.115: D/CLIPBOARD(16518): Hide Clipboard dialog at Starting input: finished by someone else... !
05-19 23:23:37.760: D/CLIPBOARD(16518): Hide Clipboard dialog at Starting input: finished by someone else... !
05-19 23:28:52.000: D/AndroidRuntime(16834): Shutting down VM
05-19 23:28:52.000: W/dalvikvm(16834): threadid=1: thread exiting with uncaught exception (group=0x40192760)
05-19 23:28:52.000: E/AndroidRuntime(16834): FATAL EXCEPTION: main
05-19 23:28:52.000: E/AndroidRuntime(16834): java.lang.RuntimeException: Unable to start activity ComponentInfocom.example2.mysecondapp/com.example2.mysecondapp.StartingPoint: java.lang.NullPointerException
05-19 23:28:52.000: E/AndroidRuntime(16834):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
05-19 23:28:52.000: E/AndroidRuntime(16834):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
05-19 23:28:52.000: E/AndroidRuntime(16834):    at android.app.ActivityThread.access$500(ActivityThread.java:122)
05-19 23:28:52.000: E/AndroidRuntime(16834):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
05-19 23:28:52.000: E/AndroidRuntime(16834):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 23:28:52.000: E/AndroidRuntime(16834):    at android.os.Looper.loop(Looper.java:132)
05-19 23:28:52.000: E/AndroidRuntime(16834):    at android.app.ActivityThread.main(ActivityThread.java:4123)
05-19 23:28:52.000: E/AndroidRuntime(16834):    at java.lang.reflect.Method.invokeNative(Native Method)
05-19 23:28:52.000: E/AndroidRuntime(16834):    at java.lang.reflect.Method.invoke(Method.java:491)
05-19 23:28:52.000: E/AndroidRuntime(16834):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
05-19 23:28:52.000: E/AndroidRuntime(16834):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
05-19 23:28:52.000: E/AndroidRuntime(16834):    at dalvik.system.NativeStart.main(Native Method)
05-19 23:28:52.000: E/AndroidRuntime(16834): Caused by: java.lang.NullPointerException
05-19 23:28:52.000: E/AndroidRuntime(16834):    at com.example2.mysecondapp.StartingPoint.onCreate(StartingPoint.java:47)
05-19 23:28:52.000: E/AndroidRuntime(16834):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
05-19 23:28:52.000: E/AndroidRuntime(16834):    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
05-19 23:28:52.000: E/AndroidRuntime(16834):    ... 11 more
05-19 23:28:59.720: I/Process(16834): Sending signal. PID: 16834 SIG: 9

我并不确切知道需要什么,所以我只发布了我认为相关的所有内容,我对此仍然很陌生。非常感谢任何帮助。

【问题讨论】:

第 47 行的 StartingPoint.java 上有什么内容? NullPointerException StartingPoint.java:47 fragment_starting_point.xml中的全部内容复制到activity_main.xml 或将setContentView(R.layout.activity_starting_point);改为setContentView(R.layout.fragment_starting_point); 检查我的答案或临时解决方案尝试我之前所说的评论...... 【参考方案1】:

Fragment 的布局包含 ID 为 R.id.bAddButtonActivity 的布局不包含。这就是你的错误的根源,这一行:

add = (Button)this.findViewById(R.id.bAdd);

Activity 的布局中找不到Button,因此它返回null。之后,当您尝试设置 OnClickListener 时,您会得到一个 NullPointerException

您需要做的就是将与Buttons 等的所有交互移动到不属于ActivityFragment 中,因为它不是Activity 布局的一部分。覆盖 onCreateView() 中的 onCreateView() 方法并在那里扩展您的布局,然后像这样查找 Views

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    View view = inflater.inflate(R.layout.fragment_starting_point, container, false);

    this.add = (Button) view.findViewById(R.id.bAdd);

    return view;

然后您可以在onViewCreated() 中添加您的OnClickListener

@Override
public void onViewCreated(View view, Bundle savedInstanceState) 
    super.onViewCreated(view, savedInstanceState);

    this.add.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            ...    
        
    );

【讨论】:

非常感谢!!它现在可以工作了 :) 感谢您也解释了一切。【参考方案2】:

这是因为findViewById()activity_main 布局中搜索,而按钮位于片段的布局fragment_main 中。

在片段的onCreateView() 方法中移动那段代码:

//...

View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button buttonClick = (Button)rootView.findViewById(R.id.button);
buttonClick.setOnClickListener(new View.OnClickListener() 
    @Override
    public void onClick(View view) 
        onButtonClick((Button) view);
    
);

请注意,现在您通过rootView view 访问它:

Button buttonClick = (Button)rootView.findViewById(R.id.button);

否则你会再次收到NullPointerException

【讨论】:

试一试..@MandiraSawkar...试试这个答案或临时解决方案试试我的 cmets。 你试过了吗..??? @MandiraSawkar ???? 谢谢!这也有效。

以上是关于setOnClickListener 使我的应用程序崩溃 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

WebView 使用 VALIDATION_ERROR_DESERIALIZATION_FAILED 使我的应用程序崩溃

如何使我的 Android 应用符合“后台位置政策”

为啥我的 ListView 使我的应用程序崩溃而不是列出我的条目?

我想使我的 mfc 应用程序崩溃

为啥意图使我的应用程序崩溃?

为啥我的 unwind segue 会使我的应用程序崩溃?