为新活动制作按钮

时间:2014-04-11 02:13:34

标签: android button android-activity

我正在创建我的第一个应用程序,我正在尝试创建一个按钮,将用户带到下一个活动。我研究了如何做到这一点并找到了我关注的视频(https://www.youtube.com/watch?v=fFOPzmeknNo)。在尝试在模拟器上运行之前,我似乎没有任何问题。在模拟器上,它说应用程序已停止工作。在logcat中我得到了这个。

04-10 22:10:10.918: D/AndroidRuntime(1974): Shutting down VM
04-10 22:10:10.918: W/dalvikvm(1974): threadid=1: thread exiting with uncaught exception (group=0xb2ae0ba8)
04-10 22:10:10.938: E/AndroidRuntime(1974): FATAL EXCEPTION: main
04-10 22:10:10.938: E/AndroidRuntime(1974): Process: com.example.first, PID: 1974
04-10 22:10:10.938: E/AndroidRuntime(1974): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first/com.example.first.MainActivity}: java.lang.NullPointerException
04-10 22:10:10.938: E/AndroidRuntime(1974):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-10 22:10:10.938: E/AndroidRuntime(1974):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-10 22:10:10.938: E/AndroidRuntime(1974):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-10 22:10:10.938: E/AndroidRuntime(1974):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-10 22:10:10.938: E/AndroidRuntime(1974):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-10 22:10:10.938: E/AndroidRuntime(1974):     at android.os.Looper.loop(Looper.java:136)
04-10 22:10:10.938: E/AndroidRuntime(1974):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-10 22:10:10.938: E/AndroidRuntime(1974):     at java.lang.reflect.Method.invokeNative(Native Method)
04-10 22:10:10.938: E/AndroidRuntime(1974):     at java.lang.reflect.Method.invoke(Method.java:515)
04-10 22:10:10.938: E/AndroidRuntime(1974):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-10 22:10:10.938: E/AndroidRuntime(1974):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-10 22:10:10.938: E/AndroidRuntime(1974):     at dalvik.system.NativeStart.main(Native Method)
04-10 22:10:10.938: E/AndroidRuntime(1974): Caused by: java.lang.NullPointerException
04-10 22:10:10.938: E/AndroidRuntime(1974):     at com.example.first.MainActivity.onCreate(MainActivity.java:27)
04-10 22:10:10.938: E/AndroidRuntime(1974):     at android.app.Activity.performCreate(Activity.java:5231)
04-10 22:10:10.938: E/AndroidRuntime(1974):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-10 22:10:10.938: E/AndroidRuntime(1974):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-10 22:10:10.938: E/AndroidRuntime(1974):     ... 11 more

我环顾四周但一直无法找到解决方案。任何帮助是极大的赞赏。谢谢!

我的创造方法(我希望)

 protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    play = (Button)findViewById(R.id.play);
    play.setOnClickListener(this);

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

private void playClick()
{
    startActivity(new Intent("this,Maps.class"));
}

不确定这是否正是您所寻找的,但我希望它有所帮助。

@Override
public void onClick(View v) {
    switch (v.getId())
    {
    case R.id.play:
        playClick();
        break;
    }

3 个答案:

答案 0 :(得分:0)

我认为你得到空指针,而你试图通过这段代码将你的活动带到另一个活动:

startActivity(new Intent("this,Maps.class"));

要将您的活动带到另一项活动中,您不应该将"置于下面:

startActivity(new Intent(this,Maps.class));

答案 1 :(得分:0)

您应该将按钮放在onCreateView方法中。因为它是关注的片段布局。这些行:

play = (Button)findViewById(R.id.play);
play.setOnClickListener(this);

需要进入你的片段。但首先,请删除您活动的implement OnClickListener

implements OnClickListener {..}  

然后,在Fragment中找到您的按钮并调用新活动:

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

    // Here, you will find these views with its ids 
    play = (Button) rootView.findViewById(R.id.play);

    // DON'T FORGET TO ATTACH THE VIEWS TO THE INFLATED VIEW
    // HERE IT'S "rootView.findViewById(R.id...)"  

    play.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // do your stuff
        }  
    });
}

此外,您的Intent有错误:您不能使用像“myclass.this,second.class”这样的字符串......您应该使用名称类(不带引号) )如下:

// First the activity where you are now, second the activity class you want to go
startActivity(new Intent(MainActivity.this, Maps.class)); 

但是,在片段中,您需要将父活动附加为getActivity()

@Override
public void onClick(View v) {
    // Attach the parent and go to Maps activity
    startActivity(new Intent(getActivity(), Maps.class));
} 

答案 2 :(得分:0)

首先你必须在oncreate方法外创建一个私有按钮变量。

  private Button play.

然后在oncreate方法中添加此代码。   您当前的类是您的java类名称

play= (Button) findViewById(R.id.play);
play.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent intent_map = new Intent(yourcurrentclass.this, Maps.class);
                startActivity(intent_home);
                finish();

        }
    });