问题描述
程序功能正常就开始了。当按钮被点击,应用程序强制关闭和日志猫(底部)所示。
XML文件 - 布局是全功能的
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=$ com.example.figurehowtodo.MainActivity PlaceholderFragment><的TextView
机器人:ID =@ + ID / produceText1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/参考hello world/><按钮
机器人:ID =@ + ID / myactualbutton
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:文字=ClickMe/>< / LinearLayout中>
MainActivity.java文件
包com.example.figurehowtodo;进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.Button;
进口android.widget.TextView;公共类MainActivity延伸活动{按钮myButton的;
TextView的tvView;保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.fragment_main);
则myButton =(按钮)findViewById(R.id.myactualbutton);
tvView =(的TextView)findViewById(R.id.produceText1);
mybutton.setOnClickListener(新MyOwnOnClickListener());}
}
MyOwnOnClickListener.java
包com.example.figurehowtodo;进口android.app.Activity;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.TextView;公共类MyOwnOnClickListener扩展活动实现OnClickListener {
// INT ID;
TextView的ID2;
// TextView的ID3;/ * MainActivity来电者;
公共MyOwnOnClickListener(){
加成();
忽略此位
} * /公共无效的onClick(视图v){
添加(五);
}公共无效添加(视图v){
// ID = hello.getId();
ID2 =(的TextView)findViewById(R.id.produceText1); id2.setText(fgdsgdfhgdfh);
}}
logcat的LOG
20 03-27:56:23.831:D / AndroidRuntime(2109):>>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit<<<<<<
03-27 20:56:23.831:D / AndroidRuntime(2109):CheckJNI为ON
03-27 20:56:24.280:D / AndroidRuntime(2109):在主入口com.android.commands.pm.Pm
03-27 20:56:24.390:D / dalvikvm(299):GC_EXPLICIT释放4K,54%免费2545K / 5511K,1625K外部/ 2137K,暂停51ms
03-27 20:56:24.390:W / ActivityManager(61):没有内容提供商发现:
03-27 20:56:24.491:W / ActivityManager(61):没有内容提供商发现:
03-27 20:56:24.500:D / PackageParser(61):扫描包:/data/app/vmdl58109320.tmp
03-27 20:56:24.810:D / dalvikvm(61):GC_CONCURRENT释放1447K,46%免费5086K / 9287K,3520K外部/ 4385K,暂停为5ms + 6ms的
03-27 20:56:25.020:D / dalvikvm(61):GC_FOR_MALLOC释放691K,47%免费4979K / 9287K,3511K外部/ 4385K,暂停的55ms
03-27 20:56:25.200:D / dalvikvm(61):GC_FOR_MALLOC释放634K,47%免费4982K / 9287K,3511K外部/ 4385K,暂停56MS
03-27 20:56:25.381:D / dalvikvm(61):GC_FOR_MALLOC释放632K,47%免费4985K / 9287K,3511K外部/ 4385K,暂停54ms
03-27 20:56:25.560:D / dalvikvm(61):GC_FOR_MALLOC释放633K,47%免费4990K / 9287K,3511K外部/ 4385K,暂停54ms
03-27 20:56:25.700:I /软件包管理系统(61):删除非系统包:com.example.figurehowtodo
03-27 20:56:25.700:I / ActivityManager(61):强制停止包com.example.figurehowtodo的uid = 10037
03-27 20:56:25.760:D /软件包管理系统(61):扫描包com.example.figurehowtodo
03-27 20:56:25.770:I /软件包管理系统(61):包com.example.figurehowtodo codePATH从/data/app/com.example.figurehowtodo-1.apk改为/数据/应用/ COM。 example.figurehowtodo-2.apk;保存数据,并使用新
03-27 20:56:25.770:I /软件包管理系统(61):拆包/data/app/com.example.figurehowtodo-2.apk本地库
03-27 20:56:25.800:D / INSTALLD(35):DexInv:--- BEGIN/data/app/com.example.figurehowtodo-2.apk'---
03-27 20:56:26.620:D / dalvikvm(2118):DexOpt:找不到场Landroid /图形/ BitmapFactory $选项; .inMutable
03-27 20:56:26.861:D / dalvikvm(2118):DexOpt:找不到静态字段
03-27 20:56:27.330:D / dalvikvm(2118):DexOpt:负载260ms,验证+选择850ms
03-27 20:56:27.380:D / INSTALLD(35):DexInv:--- END/data/app/com.example.figurehowtodo-2.apk(成功)---
03-27 20:56:27.380:W /软件包管理系统(61):为PKG code路线:从com.example.figurehowtodo改变/data/app/com.example.figurehowtodo-1.apk到/数据/应用/com.example.figurehowtodo-2.apk
03-27 20:56:27.380:W /软件包管理系统(61):为PKG资源路径:从com.example.figurehowtodo改变/data/app/com.example.figurehowtodo-1.apk到/数据/应用/ COM。 example.figurehowtodo-2.apk
03-27 20:56:27.380:D /软件包管理系统(61):活动:com.example.figurehowtodo.MainActivity
03-27 20:56:27.400:I / ActivityManager(61):强制停止包com.example.figurehowtodo的uid = 10037
03-27 20:56:27.470:I / INSTALLD(35):移动/data/dalvik-cache/data@[email protected]@classes.dex - > /data/dalvik-cache/data@[email protected]@classes.dex
03-27 20:56:27.470:安装在/data/app/com.example.figurehowtodo-2.apk新包装:D /软件包管理系统(61)
03-27 20:56:27.560:I / ActivityManager(61):强制停止包com.example.figurehowtodo的uid = 10037
03-27 20:56:27.620:D / dalvikvm(134):GC_EXPLICIT释放101K,50%的自由2951K / 5895K,5981K外部/ 7371K,暂停54ms
03-27 20:56:27.700:D / dalvikvm(177):GC_EXPLICIT释放97K,52%免费2759K / 5639K,1625K外部/ 2137K,暂停73ms
03-27 20:56:27.770:W / RecognitionManagerService(61):发现没有可用的语音识别服务
03-27 20:56:28.080:D / dalvikvm(61):GC_EXPLICIT释放573K,46%免费5081K / 9287K,3511K外部/ 4385K,暂停96ms
03-27 20:56:28.120:I / INSTALLD(35):断开链接/data/dalvik-cache/data@[email protected]@classes.dex
03-27 20:56:28.120:D / AndroidRuntime(2109):关闭VM
03-27 20:56:28.141:D / dalvikvm(2109):GC_CONCURRENT释放101K,72%的免费295K / 1024K,外部0K / 0K,暂停1毫秒+ 1毫秒
03-27 20:56:28.141:D / JDWP(2109):GOT唤醒信号,选择的拯救
03-27 20:56:28.141:D / dalvikvm(2109):调试器脱落;对象注册了1项
03-27 20:56:28.470:D / AndroidRuntime(2124):GT;>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit<<<<<<
03-27 20:56:28.470:D / AndroidRuntime(2124):CheckJNI为ON
03-27 20:56:28.920:D / AndroidRuntime(2124):在主入口com.android.commands.am.Am
03-27 20:56:28.940:I / ActivityManager(61):开始:意向{行动= android.intent.action.MAIN猫= [android.intent.category.LAUNCHER] FLG = 0x10000000的CMP = com.example.figurehowtodo /.MainActivity}从PID 2124
03-27 20:56:29.111:D / AndroidRuntime(2124):关闭VM
03-27 20:56:29.111:I / ActivityManager(61):启动PROC com.example.figurehowtodo的活动com.example.figurehowtodo / .MainActivity:PID = 2132 UID = 10037导报= {}
03-27 20:56:29.130:D / dalvikvm(2124):GC_CONCURRENT释放102K,69%免费320K / 1024K,外部0K / 0K,暂停1毫秒+ 1毫秒
03-27 20:56:29.140:D / JDWP(2124):GOT唤醒信号,选择的拯救
03-27 20:56:29.140:D / dalvikvm(2124):调试器脱落;对象注册了1项
03-27 20:56:29.180:I / dalvikvm(2124):JNI:AttachCurrentThread(从??? ???)。
03-27 20:56:29.180:I / AndroidRuntime(2124):注:连接线的捆扎线#3'失败
03-27 20:56:29.930:D / dalvikvm(115):GREF已增加至201
03-27 20:56:30.000:I / ActivityManager(61):显示com.example.figurehowtodo / .MainActivity:+ 897ms
03-27 20:56:31.690:D / AndroidRuntime(2132):关闭VM
03-27 20:56:31.690:W / dalvikvm(2132):主题ID = 1:螺纹未捕获的异常(组= 0x40015560)退出
----------错误就从这里开始-------错误就从这里开始-------错误就从这里开始-------
20 03-27:56:31.710:E / AndroidRuntime(2132):致命异常:主要
03-27 20:56:31.710:E / AndroidRuntime(2132):显示java.lang.NullPointerException
03-27 20:56:31.710:E / AndroidRuntime(2132):在android.app.Activity.findViewById(Activity.java:1647)
03-27 20:56:31.710:E / AndroidRuntime(2132):在com.example.figurehowtodo.MyOwnOnClickListener.addiTion(MyOwnOnClickListener.java:29)
03-27 20:56:31.710:E / AndroidRuntime(2132):在com.example.figurehowtodo.MyOwnOnClickListener.onClick(MyOwnOnClickListener.java:22)
03-27 20:56:31.710:E / AndroidRuntime(2132):在android.view.View.performClick(View.java:2485)
03-27 20:56:31.710:E / AndroidRuntime(2132):在android.view.View $ PerformClick.run(View.java:9080)
03-27 20:56:31.710:E / AndroidRuntime(2132):在android.os.Handler.handleCallback(Handler.java:587)
03-27 20:56:31.710:E / AndroidRuntime(2132):在android.os.Handler.dispatchMessage(Handler.java:92)
03-27 20:56:31.710:E / AndroidRuntime(2132):在android.os.Looper.loop(Looper.java:123)
03-27 20:56:31.710:E / AndroidRuntime(2132):在android.app.ActivityThread.main(ActivityThread.java:3683)
03-27 20:56:31.710:E / AndroidRuntime(2132):在java.lang.reflect.Method.invokeNative(本机方法)
03-27 20:56:31.710:E / AndroidRuntime(2132):在java.lang.reflect.Method.invoke(Method.java:507)
03-27 20:56:31.710:E / AndroidRuntime(2132):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
03-27 20:56:31.710:E / AndroidRuntime(2132):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-27 20:56:31.710:E / AndroidRuntime(2132):在dalvik.system.NativeStart.main(本机方法)
03-27 20:56:31.720:W / ActivityManager(61):强制完成活动com.example.figurehowtodo / .MainActivity
03-27 20:56:32.230:W / ActivityManager(61):活动暂停超时HistoryRecord {40698748 com.example.figurehowtodo / .MainActivity}
03-27 20:56:38.440:D / dalvikvm(299):GC_EXPLICIT释放6K,54%免费2544K / 5511K,1625K外部/ 2137K,暂停的55ms
03-27 20:56:43.380:W / ActivityManager(61):活动破坏超时HistoryRecord {40698748 com.example.figurehowtodo / .MainActivity}
03-27 20:56:43.480:D / dalvikvm(311):GC_EXPLICIT释放2K,54%免费2537K / 5511K,1625K外部/ 2137K,暂停78ms
03-27 20:56:48.460:D / dalvikvm(220):GC_EXPLICIT释放184K,54%免费2764K / 5959K,2202K外部/ 2671K,暂停56MS
您还没有设置您的内容查看您的活动类 MyOwnOnClickListener
。所以方法 findViewById()
找不到你传递作为参数的看法。
我觉得 MyOwnOnClickListener
类不应该是一个活动
类,而只是一个倾听者。你可以传递到的TextView
A参考监听器来使用它。
或者你可以实现你的 MainActivity
类和SUP preSS的 MyOwnOnClickListener
类。
公共类MainActivity扩展活动实现OnClickListener {
按钮myButton的;
TextView的tvView; 保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.fragment_main);
则myButton =(按钮)findViewById(R.id.myactualbutton);
tvView =(的TextView)findViewById(R.id.produceText1);
mybutton.setOnClickListener(本);
} 公共无效的onClick(视图v){
添加(五);
} 公共无效添加(视图v){
tvView.setText(fgdsgdfhgdfh);
}
}
编辑:如果你想使用一个监听器类,只是保持你的 MainActivity
,因为它是只改变调用 MyOwnOnClickListener
来传递 tvView
参考,并修改 MyOwnOnClickListener
类是这样的:
公共类MyOwnOnClickListener实现OnClickListener {
TextView的ID2; // MainActivity呼叫者;
公共MyOwnOnClickListener(ID2的TextView){
this.id2 = ID2;
} 公共无效的onClick(视图v){
开关(v.getId()){
案例R.id.produceText1:
id2.setText(fgdsgdfhgdfh);
打破;
//这里的其他情形...
默认:
//无论你要管理的情况......
}
}
}
它应该工作...
Program functions as normal on start up. As soon as button is clicked, app force closes and the log cat (at bottom) is shown.
XML File - layout is fully functional
<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 file
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 LOG
03-27 20:56:23.831: D/AndroidRuntime(2109): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
03-27 20:56:23.831: D/AndroidRuntime(2109): CheckJNI is ON
03-27 20:56:24.280: D/AndroidRuntime(2109): Calling main entry com.android.commands.pm.Pm
03-27 20:56:24.390: D/dalvikvm(299): GC_EXPLICIT freed 4K, 54% free 2545K/5511K, external 1625K/2137K, paused 51ms
03-27 20:56:24.390: W/ActivityManager(61): No content provider found for:
03-27 20:56:24.491: W/ActivityManager(61): No content provider found for:
03-27 20:56:24.500: D/PackageParser(61): Scanning package: /data/app/vmdl58109320.tmp
03-27 20:56:24.810: D/dalvikvm(61): GC_CONCURRENT freed 1447K, 46% free 5086K/9287K, external 3520K/4385K, paused 5ms+6ms
03-27 20:56:25.020: D/dalvikvm(61): GC_FOR_MALLOC freed 691K, 47% free 4979K/9287K, external 3511K/4385K, paused 55ms
03-27 20:56:25.200: D/dalvikvm(61): GC_FOR_MALLOC freed 634K, 47% free 4982K/9287K, external 3511K/4385K, paused 56ms
03-27 20:56:25.381: D/dalvikvm(61): GC_FOR_MALLOC freed 632K, 47% free 4985K/9287K, external 3511K/4385K, paused 54ms
03-27 20:56:25.560: D/dalvikvm(61): GC_FOR_MALLOC freed 633K, 47% free 4990K/9287K, external 3511K/4385K, paused 54ms
03-27 20:56:25.700: I/PackageManager(61): Removing non-system package:com.example.figurehowtodo
03-27 20:56:25.700: I/ActivityManager(61): Force stopping package com.example.figurehowtodo uid=10037
03-27 20:56:25.760: D/PackageManager(61): Scanning package com.example.figurehowtodo
03-27 20:56:25.770: I/PackageManager(61): Package com.example.figurehowtodo codePath changed from /data/app/com.example.figurehowtodo-1.apk to /data/app/com.example.figurehowtodo-2.apk; Retaining data and using new
03-27 20:56:25.770: I/PackageManager(61): Unpacking native libraries for /data/app/com.example.figurehowtodo-2.apk
03-27 20:56:25.800: D/installd(35): DexInv: --- BEGIN '/data/app/com.example.figurehowtodo-2.apk' ---
03-27 20:56:26.620: D/dalvikvm(2118): DexOpt: couldn't find field Landroid/graphics/BitmapFactory$Options;.inMutable
03-27 20:56:26.861: D/dalvikvm(2118): DexOpt: couldn't find static field
03-27 20:56:27.330: D/dalvikvm(2118): DexOpt: load 260ms, verify+opt 850ms
03-27 20:56:27.380: D/installd(35): DexInv: --- END '/data/app/com.example.figurehowtodo-2.apk' (success) ---
03-27 20:56:27.380: W/PackageManager(61): Code path for pkg : com.example.figurehowtodo changing from /data/app/com.example.figurehowtodo-1.apk to /data/app/com.example.figurehowtodo-2.apk
03-27 20:56:27.380: W/PackageManager(61): Resource path for pkg : com.example.figurehowtodo changing from /data/app/com.example.figurehowtodo-1.apk to /data/app/com.example.figurehowtodo-2.apk
03-27 20:56:27.380: D/PackageManager(61): Activities: com.example.figurehowtodo.MainActivity
03-27 20:56:27.400: I/ActivityManager(61): Force stopping package com.example.figurehowtodo uid=10037
03-27 20:56:27.470: I/installd(35): move /data/dalvik-cache/data@[email protected]@classes.dex -> /data/dalvik-cache/data@[email protected]@classes.dex
03-27 20:56:27.470: D/PackageManager(61): New package installed in /data/app/com.example.figurehowtodo-2.apk
03-27 20:56:27.560: I/ActivityManager(61): Force stopping package com.example.figurehowtodo uid=10037
03-27 20:56:27.620: D/dalvikvm(134): GC_EXPLICIT freed 101K, 50% free 2951K/5895K, external 5981K/7371K, paused 54ms
03-27 20:56:27.700: D/dalvikvm(177): GC_EXPLICIT freed 97K, 52% free 2759K/5639K, external 1625K/2137K, paused 73ms
03-27 20:56:27.770: W/RecognitionManagerService(61): no available voice recognition services found
03-27 20:56:28.080: D/dalvikvm(61): GC_EXPLICIT freed 573K, 46% free 5081K/9287K, external 3511K/4385K, paused 96ms
03-27 20:56:28.120: I/installd(35): unlink /data/dalvik-cache/data@[email protected]@classes.dex
03-27 20:56:28.120: D/AndroidRuntime(2109): Shutting down VM
03-27 20:56:28.141: D/dalvikvm(2109): GC_CONCURRENT freed 101K, 72% free 295K/1024K, external 0K/0K, paused 1ms+1ms
03-27 20:56:28.141: D/jdwp(2109): Got wake-up signal, bailing out of select
03-27 20:56:28.141: D/dalvikvm(2109): Debugger has detached; object registry had 1 entries
03-27 20:56:28.470: D/AndroidRuntime(2124): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
03-27 20:56:28.470: D/AndroidRuntime(2124): CheckJNI is ON
03-27 20:56:28.920: D/AndroidRuntime(2124): Calling main entry com.android.commands.am.Am
03-27 20:56:28.940: I/ActivityManager(61): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.figurehowtodo/.MainActivity } from pid 2124
03-27 20:56:29.111: D/AndroidRuntime(2124): Shutting down VM
03-27 20:56:29.111: I/ActivityManager(61): Start proc com.example.figurehowtodo for activity com.example.figurehowtodo/.MainActivity: pid=2132 uid=10037 gids={}
03-27 20:56:29.130: D/dalvikvm(2124): GC_CONCURRENT freed 102K, 69% free 320K/1024K, external 0K/0K, paused 1ms+1ms
03-27 20:56:29.140: D/jdwp(2124): Got wake-up signal, bailing out of select
03-27 20:56:29.140: D/dalvikvm(2124): Debugger has detached; object registry had 1 entries
03-27 20:56:29.180: I/dalvikvm(2124): JNI: AttachCurrentThread (from ???.???)
03-27 20:56:29.180: I/AndroidRuntime(2124): NOTE: attach of thread 'Binder Thread #3' failed
03-27 20:56:29.930: D/dalvikvm(115): GREF has increased to 201
03-27 20:56:30.000: I/ActivityManager(61): Displayed com.example.figurehowtodo/.MainActivity: +897ms
03-27 20:56:31.690: D/AndroidRuntime(2132): Shutting down VM
03-27 20:56:31.690: W/dalvikvm(2132): threadid=1: thread exiting with uncaught exception (group=0x40015560)
----------ERROR BEGINS HERE-------ERROR BEGINS HERE------- ERROR BEGINS HERE-------
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
You have not set you content view for your activity class MyOwnOnClickListener
. So method findViewById()
cannot find the view you are passing as argument.
I think the MyOwnOnClickListener
class shouldn't be an Activity
class, but just a listener. You can pass a reference to the TextView
to the listener to use it.
Or you can implement the listener in your MainActivity
class and suppress the MyOwnOnClickListener
class.
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");
}
}
EDIT: If you want to use a listener class, just keep your MainActivity
as it is, change only the call to the MyOwnOnClickListener
to pass the tvView
reference, and change the MyOwnOnClickListener
class like this:
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...
}
}
}
It should work...
这篇关于Android应用程序强制关闭 - setOnClickListener [简单的应用程序] [新手]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!