在一个单独的类中实现一个监听器

时间:2013-02-07 10:35:51

标签: android android-manifest listener

我在单独的类中实现按钮侦听器时遇到问题。代码:

<Button 
    android:id ="@+button/bottone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text = "Button"
    />

主要班级:

public class MainActivity extends Activity {

private final Context main_context = this.getApplicationContext(); 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Button b = (Button)findViewById(R.button.bottone);
    Log.i("AAAAAAAAAAAA", "1");

    OnClickListener l = new MyClickListener(this);
    b.setOnClickListener(l);
    Log.i("AAAAAAAAAAAA", "2");


    setContentView(R.layout.activity_main);
}

public Context getContext() {
    return main_context;
}

和监听器的实现

public class MyClickListener implements OnClickListener {
private final Context cntx;

public MyClickListener(Context c) {
    cntx = c;
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Log.i("ONCLICK", "bottone cliccato");
    Toast t = Toast.makeText(cntx, "OK", Toast.LENGTH_LONG);
    t.show();
}

现在,当我尝试运行它时,应用程序在我的手机上崩溃,而logcat告诉

  

无法实例化活动组件信息   关于主动性。

出了什么问题?

logcat信息的日志

  

02-07 11:43:54.887:E / AndroidRuntime(9048):致命异常:主要   02-07:43:54.887:E / AndroidRuntime(9048):java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.buttoneventlistener / com.example.buttoneventlistener.MainActivity}:java.lang.NullPointerException   02-07 11:43:54.887:E / AndroidRuntime(9048):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)   02-07 11:43:54.887:E / AndroidRuntime(9048):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)   02-07 11:43:54.887:E / AndroidRuntime(9048):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)   02-07:43:54.887:E / AndroidRuntime(9048):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)   02-07 11:43:54.887:E / AndroidRuntime(9048):在android.os.Handler.dispatchMessage(Handler.java:99)   02-07 11:43:54.887:E / AndroidRuntime(9048):在android.os.Looper.loop(Looper.java:137)   02-07 11:43:54.887:E / AndroidRuntime(9048):在android.app.ActivityThread.main(ActivityThread.java:5039)   02-07 11:43:54.887:E / AndroidRuntime(9048):at java.lang.reflect.Method.invokeNative(Native Method)   02-07 11:43:54.887:E / AndroidRuntime(9048):at java.lang.reflect.Method.invoke(Method.java:511)   02-07 11:43:54.887:E / AndroidRuntime(9048):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)   02-07 11:43:54.887:E / AndroidRuntime(9048):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)   02-07 11:43:54.887:E / AndroidRuntime(9048):at dalvik.system.NativeStart.main(Native Method)   02-07 11:43:54.887:E / AndroidRuntime(9048):引起:java.lang.NullPointerException   02-07 11:43:54.887:E / AndroidRuntime(9048):在android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)   02-07 11:43:54.887:E / AndroidRuntime(9048):at com.example.buttoneventlistener.MainActivity。(MainActivity.java:13)   02-07 11:43:54.887:E / AndroidRuntime(9048):at java.lang.Class.newInstanceImpl(Native Method)   02-07 11:43:54.887:E / AndroidRuntime(9048):at java.lang.Class.newInstance(Class.java:1319)   02-07 11:43:54.887:E / AndroidRuntime(9048):在android.app.Instrumentation.newActivity(Instrumentation.java:1054)   02-07 11:43:54.887:E / AndroidRuntime(9048):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)   02-07 11:43:54.887:E / AndroidRuntime(9048):... 11更多

2 个答案:

答案 0 :(得分:3)

在从UI访问按钮之前移动setContentView(R.layout.activity_main);行:

public class MainActivity extends Activity {

private  Context main_context; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);  //<< set Activity layout here

    main_context = this.getApplicationContext(); 
    Button b = (Button)findViewById(R.id.bottone);
    Log.i("AAAAAAAAAAAA", "1");

    OnClickListener l = new MyClickListener(this);
    b.setOnClickListener(l);
    Log.i("AAAAAAAAAAAA", "2");



}

答案 1 :(得分:2)

编辑:

R.button.bottone更改为R.id.bottone,将android:id ="@+button/bottone"更改为android:id ="@+id/bottone"

获取按钮对象之前的

setContentView,否则按钮对象返回null,访问但会导致NullPointerException。   setContentView(R.layout.activity_main);在创建按钮之前放置