程序在启动时正常运行。单击按钮后,app force将立即关闭,并显示日志cat(位于底部)。
XML文件 - 布局功能齐全
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.figurehowtodo.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/produceText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/myactualbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ClickMe" />
</LinearLayout>
MainActivity.java
package com.example.figurehowtodo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button mybutton;
TextView tvView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
mybutton = (Button) findViewById(R.id.myactualbutton);
tvView = (TextView) findViewById(R.id.produceText1);
mybutton.setOnClickListener(new MyOwnOnClickListener());
}
}
MyOwnOnClickListener.java
package com.example.figurehowtodo;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MyOwnOnClickListener extends Activity implements OnClickListener{
//int id;
TextView id2;
//TextView id3;
/* MainActivity caller;
public MyOwnOnClickListener() {
addiTion();
IGNORE THIS BIT
} */
public void onClick(View v) {
addiTion(v);
}
public void addiTion(View v){
//id = hello.getId();
id2 = (TextView) findViewById(R.id.produceText1);
id2.setText("fgdsgdfhgdfh");
}
}
logcat的:
03-27 20:56:31.710: E/AndroidRuntime(2132): FATAL EXCEPTION: main
03-27 20:56:31.710: E/AndroidRuntime(2132): java.lang.NullPointerException
03-27 20:56:31.710: E/AndroidRuntime(2132): at android.app.Activity.findViewById(Activity.java:1647)
03-27 20:56:31.710: E/AndroidRuntime(2132): at com.example.figurehowtodo.MyOwnOnClickListener.addiTion(MyOwnOnClickListener.java:29)
03-27 20:56:31.710: E/AndroidRuntime(2132): at com.example.figurehowtodo.MyOwnOnClickListener.onClick(MyOwnOnClickListener.java:22)
03-27 20:56:31.710: E/AndroidRuntime(2132): at android.view.View.performClick(View.java:2485)
03-27 20:56:31.710: E/AndroidRuntime(2132): at android.view.View$PerformClick.run(View.java:9080)
03-27 20:56:31.710: E/AndroidRuntime(2132): at android.os.Handler.handleCallback(Handler.java:587)
03-27 20:56:31.710: E/AndroidRuntime(2132): at android.os.Handler.dispatchMessage(Handler.java:92)
03-27 20:56:31.710: E/AndroidRuntime(2132): at android.os.Looper.loop(Looper.java:123)
03-27 20:56:31.710: E/AndroidRuntime(2132): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-27 20:56:31.710: E/AndroidRuntime(2132): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 20:56:31.710: E/AndroidRuntime(2132): at java.lang.reflect.Method.invoke(Method.java:507)
03-27 20:56:31.710: E/AndroidRuntime(2132): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-27 20:56:31.710: E/AndroidRuntime(2132): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-27 20:56:31.710: E/AndroidRuntime(2132): at dalvik.system.NativeStart.main(Native Method)
03-27 20:56:31.720: W/ActivityManager(61): Force finishing activity com.example.figurehowtodo/.MainActivity
03-27 20:56:32.230: W/ActivityManager(61): Activity pause timeout for HistoryRecord{40698748 com.example.figurehowtodo/.MainActivity}
03-27 20:56:38.440: D/dalvikvm(299): GC_EXPLICIT freed 6K, 54% free 2544K/5511K, external 1625K/2137K, paused 55ms
03-27 20:56:43.380: W/ActivityManager(61): Activity destroy timeout for HistoryRecord{40698748 com.example.figurehowtodo/.MainActivity}
03-27 20:56:43.480: D/dalvikvm(311): GC_EXPLICIT freed 2K, 54% free 2537K/5511K, external 1625K/2137K, paused 78ms
03-27 20:56:48.460: D/dalvikvm(220): GC_EXPLICIT freed 184K, 54% free 2764K/5959K, external 2202K/2671K, paused 56ms
答案 0 :(得分:1)
您尚未为活动类MyOwnOnClickListener
设置内容视图。因此,方法findViewById()
无法找到您作为参数传递的视图。
我认为MyOwnOnClickListener
类不应该是Activity
类,而只是一个监听器。您可以将对TextView
的引用传递给侦听器以使用它。
或者您可以在MainActivity
班级中实施听众,并取消MyOwnOnClickListener
班级。
public class MainActivity extends Activity implements OnClickListener {
Button mybutton;
TextView tvView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
mybutton = (Button) findViewById(R.id.myactualbutton);
tvView = (TextView) findViewById(R.id.produceText1);
mybutton.setOnClickListener(this);
}
public void onClick(View v) {
addiTion(v);
}
public void addiTion(View v){
tvView.setText("fgdsgdfhgdfh");
}
}
编辑:如果您想使用听众课程,请保持MainActivity
不变,只更改MyOwnOnClickListener
的来电以传递tvView
参考,然后更改MyOwnOnClickListener
这样的课程:
public class MyOwnOnClickListener implements OnClickListener{
TextView id2;
// MainActivity caller;
public MyOwnOnClickListener(TextView id2) {
this.id2 = id2;
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.produceText1:
id2.setText("fgdsgdfhgdfh");
break;
// Other cases here...
default:
// Whatever you want to manage the situation...
}
}
}
它应该有用......
答案 1 :(得分:1)
替换你的
mybutton.setOnClickListener(new MyOwnOnClickListener());
通过
mybutton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
tvView.setText("fgdsgdfhgdfh");
}
});
在Java中称为匿名类。
以这种方式设置侦听器,声明实现接口 OnClickListener
的匿名类,实现其抽象方法 {{1} },创建此匿名类的对象,并将此对象传递到按钮的onClick
方法,以使您的按钮以您想要的方式继续点击它。
答案 2 :(得分:0)
使用onClick属性的替代解决方案:
将此属性添加到xml中的Button:
android:onClick="buttonClicked"
并将此方法添加到MainActivity:
public void buttonClicked(View view) {
tvView.setText("fgdsgdfhgdfh");
}
然后也删除这一行:
mybutton.setOnClickListener(new MyOwnOnClickListener());
然后完全摆脱整个MyOwnOnClickListener
活动