我不能让我的按钮工作

时间:2014-04-26 13:10:16

标签: java android button onclick listener

我只想开始在Android上制作aps,我对C ++有点了解,但是我用最简单的东西战斗了几天 - 我不能让我的按钮工作。

我尝试了每个教程方法,但我不能让听众按钮 - 每次我这样做,我的程序就会崩溃。

代码很简单(我不会在我的方法之下改变任何东西),我也开始了全新的Android项目,只是在所有教程中进行了那些按钮和TextView编辑:

package com.example.buttonwork;

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

public class MainActivity extends ActionBarActivity {
Button but1;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
    tv1 = (TextView) findViewById(R.id.tv1);
    but1 = (Button) findViewById(R.id.b1);
    but1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            tv1.setText("Button pressed");
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@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_main, container, false);
        return rootView;
    }
}

}

The LOGCAT:
04-26 15:51:39.435: W/dalvikvm(7185): threadid=1: thread exiting with uncaught exception (group=0x418762a0)
04-26 15:51:39.450: E/AndroidRuntime(7185): FATAL EXCEPTION: main
04-26 15:51:39.450: E/AndroidRuntime(7185): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttonwork/com.example.buttonwork.MainActivity}: java.lang.NullPointerException
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.os.Looper.loop(Looper.java:137)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.app.ActivityThread.main(ActivityThread.java:4921)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at java.lang.reflect.Method.invokeNative(Native Method)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at java.lang.reflect.Method.invoke(Method.java:511)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at dalvik.system.NativeStart.main(Native Method)
04-26 15:51:39.450: E/AndroidRuntime(7185): Caused by: java.lang.NullPointerException
04-26 15:51:39.450: E/AndroidRuntime(7185):     at com.example.buttonwork.MainActivity.onCreate(MainActivity.java:29)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.app.Activity.performCreate(Activity.java:5206)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at        android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
    04-26 15:51:39.450: E/AndroidRuntime(7185):     ... 11 more

程序每次都崩溃了。我已导入所有内容,ID和名称也很好。请帮助我,因为现在我很绝望。我只是想通过这些简单的事情来开始编写真正有效且有输出的东西,但我陷入了这么简单的一步......

2 个答案:

答案 0 :(得分:1)

在这里你 ..这就是你犯错误的地方。您试图获取活动中的引用而不是您定义的片段。

onCreate()占位符片段中删除按钮,添加这些组件。这就是你得到的原因空指针。

注意:使用 getActivity()来引用片段中的组件。

在你片段中,你必须做出这样的改变:

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_main, container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        Button but1 = (Button)getActivity(). findViewById(R.id.b1);
        TextView tv1 = (TextView)getActivity(). findViewById(R.id.tv1);

        but1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                tv1.setText("Button pressed");

            }
        });
    }
}

希望这会有所帮助.. :)

答案 1 :(得分:0)

使用此功能并从tb1方法中删除bt1onCreate(Bundle savedInstanceState)初始化代码

/**
 * 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_main, container, false);
 tv1 = (TextView) (rootView.findViewById(R.id.tv1));
    but1 = (Button) (rootView.findViewById(R.id.b1));
    but1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            tv1.setText("Button pressed");
        }
    });
        return rootView;
    }
}