簡體   English   中英

不幸的是,應用程序停止了Android

[英]Unfortunately application has stopped Android

我正在創建一個Android應用程序。 我面臨着“不幸的是,應用程序已停止”。 我剛剛添加了吐司活動,但無法運行模擬器。 在此之前,我只能在沒有敬酒活動的情況下查看UI。 以下是我的Java代碼。 非常感謝任何對我進步的幫助!

package com.example.test;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;





public class Test extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();

    setupMessageButton();

    }
}


public void setupMessageButton(){

    Button messageButton = (Button) findViewById(R.id.homebtn);
    messageButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(Test.this, "Clicked!", Toast.LENGTH_LONG).show();


        }
    });



}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
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_test, container,
                false);
        return rootView;
    }
}

}

下面是logcat:

D/AndroidRuntime(855): Shutting down VM
W/dalvikvm(855): threadid=1: thread exiting with uncaught exception (group=0x41465700)
E/AndroidRuntime(855): FATAL EXCEPTION: main
E/AndroidRuntime(855): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Test}: java.lang.NullPointerException
E/AndroidRuntime(855):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
E/AndroidRuntime(855):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
E/AndroidRuntime(855):at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime(855):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
E/AndroidRuntime(855):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(855):  at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(855):  at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime(855):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(855):  at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(855):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime(855):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(855):  at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(855): Caused by: java.lang.NullPointerException
E/AndroidRuntime(855):  at com.example.test.Test.setupMessageButton(Test.java:36)
E/AndroidRuntime(855):  at com.example.test.Test.onCreate(Test.java:27)
E/AndroidRuntime(855):  at android.app.Activity.performCreate(Activity.java:5133)
E/AndroidRuntime(855):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(855):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
E/AndroidRuntime(855):  ... 11 more

嘗試這個..

我猜Button homebtn屬於fragment_test.xml因此請嘗試如下操作,並刪除setupMessageButton()方法。

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_test, container,
                false);
        Button messageButton = (Button) rootView.findViewById(R.id.homebtn);
        messageButton.setOnClickListener(new View.OnClickListener() {

               @Override
               public void onClick(View v) {
                    Toast.makeText(getActivity(), "Clicked!", Toast.LENGTH_LONG).show();


               }
        });
        return rootView;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM