不幸的是,按下按钮后app停止了工作

时间:2013-12-28 19:25:19

标签: java android

我痛苦的应用程序已经不幸停止工作后我按下我创建的按钮复制textview onClick但是当我点击该按钮它崩溃的应用程序对我来说我仍然不知道是什么问题我希望你可以帮助我们

并提前感谢....

这是我的secondactivity.java

包imamalsajadsayings.android.com;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


    public class SecondActivity extends Activity {
        private Button button;
        private TextView two;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   setContentView(R.layout.second);
   button = (Button) findViewById(R.id.button2);
   button.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View v) {
           Intent i = new Intent(SecondActivity.this, MainActivity.class);
           startActivity(i);

       }
   });

   button = (Button) findViewById(R.id.button1); 
   button.setOnClickListener(new View.OnClickListener() { 
       @SuppressWarnings("deprecation") 
       public void onClick(View v) { 

           ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); 
           @SuppressWarnings("unused")
        String strCopiedText;
           int currentapiVersion = android.os.Build.VERSION.SDK_INT;
           if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB){
               ClipData clip = ClipData.newPlainText("label", "Text to Copy");
               clipboard.setPrimaryClip(clip); 

               strCopiedText = (String) clipboard.getText(); 
               clipboard.setText(two.getText().toString());

           } else{
               strCopiedText = (String) clipboard.getText(); 
               clipboard.setText(two.getText().toString());
           }
       }
   });
   }
}

这是logcat

12-28 22:19:31.502: D/dalvikvm(5571): Late-enabling CheckJNI
12-28 22:19:32.152: D/dalvikvm(5571): GC_CONCURRENT freed 177K, 4% free 6832K/7111K, paused 3ms+3ms
12-28 22:19:32.182: E/ActivityThread(5571): Failed to find provider info for com.google.plus.platform
12-28 22:19:32.202: I/dalvikvm(5571): threadid=3: reacting to signal 3
12-28 22:19:32.202: I/dalvikvm(5571): Wrote stack traces to '/data/anr/traces.txt'
12-28 22:19:32.222: I/Ads(5571): To get test ads on this device, call adRequest.addTestDevice("EC7F3FD189C6080DC9515A642B6941D1");
12-28 22:19:32.292: D/dalvikvm(5571): DexOpt: --- BEGIN 'ads804718022.jar' (bootstrap=0) ---
12-28 22:19:32.502: D/dalvikvm(5571): DexOpt: --- END 'ads804718022.jar' (success) ---
12-28 22:19:32.502: D/dalvikvm(5571): DEX prep '/data/data/imamalsajadsayings.android.com/cache/ads804718022.jar': unzip in 0ms, rewrite 217ms
12-28 22:19:32.562: D/ViewRootImpl(5571): pckname = imamalsajadsayings.android.com
12-28 22:19:32.672: I/Ads(5571): adRequestUrlHtml: <html><head><script src="http://media.admob.com/sdk-core-v40.js"></script><script>AFMA_getSdkConstants();AFMA_buildAdURL({"kw":[],"preqs":0,"session_id":"1068713454932214889","u_sd":1,"seq_num":"1","slotname":"a152b5665edfefd","u_w":800,"msid":"imamalsajadsayings.android.com","js":"afma-sdk-a-v6.4.1","ms":"pOmVaINaL4mmYKIl5TXd00nvOdskpqyV6XngIDvr1BWXJC2Qe5xyKQ-MItZHhMfJLyBWBno8NqgEAdlSzu56IG01HuqCzm9F5anA4LFdXsJJyKuzvOteSDNqnVocGIaaQbGOxNuOLLg8UfuBE56RJwaVEjyXdOKumCesOsg0o_zVLDgdeNakOjVC1DUCuZxk833qtkW78FaHLkZq-1Hvwb9UdQ6ctj7evmAhDRktOCEoEIiRHOELpWUd91p9konk32Y2kySRACgWPzL6yywFcaBRVNZVYx8Gwkdxu-EyPcD9_XRGUCPSP6sq8CdS55Dr2q2NuKKipk66AqF8C1e4rw","mv":"8011015.com.android.vending","isu":"EC7F3FD189C6080DC9515A642B6941D1","bas_off":0,"format":"320x50_mb","oar":0,"net":"wi","app_name":"4.android.imamalsajadsayings.android.com","hl":"en","ad_pos":{"height":0,"visible":0,"y":0,"x":0,"width":0},"gnt":0,"u_h":432,"pt":1,"bas_on":0,"ptime":0,"u_audio":1});</script></head><body></body></html>
12-28 22:19:32.702: I/dalvikvm(5571): threadid=3: reacting to signal 3
12-28 22:19:32.772: I/dalvikvm(5571): Wrote stack traces to '/data/anr/traces.txt'
12-28 22:19:32.802: D/dalvikvm(5571): GC_CONCURRENT freed 308K, 6% free 7006K/7431K, paused 2ms+4ms
12-28 22:19:33.202: I/dalvikvm(5571): threadid=3: reacting to signal 3
12-28 22:19:33.202: I/dalvikvm(5571): Wrote stack traces to '/data/anr/traces.txt'
12-28 22:19:33.652: D/libEGL(5571): loaded /system/lib/egl/libEGL_mali.so
12-28 22:19:33.692: D/libEGL(5571): loaded /system/lib/egl/libGLESv1_CM_mali.so
12-28 22:19:33.702: I/dalvikvm(5571): threadid=3: reacting to signal 3
12-28 22:19:33.702: I/dalvikvm(5571): Wrote stack traces to '/data/anr/traces.txt'
12-28 22:19:33.712: D/libEGL(5571): loaded /system/lib/egl/libGLESv2_mali.so
12-28 22:19:33.792: D/OpenGLRenderer(5571): Enabling debug mode 0
12-28 22:19:33.862: D/webviewglue(5571): nativeDestroy view: 0x14baab0
12-28 22:19:34.202: I/dalvikvm(5571): threadid=3: reacting to signal 3
12-28 22:19:34.232: I/dalvikvm(5571): Wrote stack traces to '/data/anr/traces.txt'
12-28 22:19:34.702: I/dalvikvm(5571): threadid=3: reacting to signal 3
12-28 22:19:34.702: I/dalvikvm(5571): Wrote stack traces to '/data/anr/traces.txt'
12-28 22:19:36.432: D/dalvikvm(5571): GC_FOR_ALLOC freed 38K, 5% free 7097K/7431K, paused 27ms
12-28 22:19:36.432: I/dalvikvm-heap(5571): Grow heap (frag case) to 7.602MB for 614416-byte allocation
12-28 22:19:36.512: D/dalvikvm(5571): GC_CONCURRENT freed 13K, 5% free 7683K/8071K, paused 3ms+6ms
12-28 22:19:36.552: D/ViewRootImpl(5571): pckname = imamalsajadsayings.android.com
12-28 22:19:36.702: D/OpenGLRenderer(5571): Flushing caches (mode 0)
12-28 22:19:37.662: I/Ads(5571): Received ad url: <url: "http://googleads.g.doubleclick.net:80/mads/gma?kw&preqs=0&session_id=1068713454932214889&u_sd=1&seq_num=1&u_w=800&msid=imamalsajadsayings.android.com&js=afma-sdk-a-v6.4.1&ms=pOmVaINaL4mmYKIl5TXd00nvOdskpqyV6XngIDvr1BWXJC2Qe5xyKQ-MItZHhMfJLyBWBno8NqgEAdlSzu56IG01HuqCzm9F5anA4LFdXsJJyKuzvOteSDNqnVocGIaaQbGOxNuOLLg8UfuBE56RJwaVEjyXdOKumCesOsg0o_zVLDgdeNakOjVC1DUCuZxk833qtkW78FaHLkZq-1Hvwb9UdQ6ctj7evmAhDRktOCEoEIiRHOELpWUd91p9konk32Y2kySRACgWPzL6yywFcaBRVNZVYx8Gwkdxu-EyPcD9_XRGUCPSP6sq8CdS55Dr2q2NuKKipk66AqF8C1e4rw&mv=8011015.com.android.vending&bas_off=0&format=320x50_mb&oar=0&net=wi&app_name=4.android.imamalsajadsayings.android.com&hl=en&gnt=0&u_h=432&bas_on=0&ptime=0&u_audio=1&imbf=%40installed_markets%40&u_so=l&output=html&region=mobile_app&u_tz=180&client_sdk=1&ex=1&slotname=a152b5665edfefd&kw_type=broad&gsb=wi&caps=inlineVideo_interactiveVideo_mraid1_th_autoplay_mediation_sdkAdmobApiForAds_di&jsv=66&urll=914" type: "admob" afmaNotifyDt: "null" activationOverlayUrl: "null" useWebViewLoadUrl: "false">
12-28 22:19:37.732: I/Ads(5571): Ad url modified to http://googleads.g.doubleclick.net:80/mads/gma?kw&preqs=0&session_id=1068713454932214889&u_sd=1&seq_num=1&u_w=800&msid=imamalsajadsayings.android.com&js=afma-sdk-a-v6.4.1&ms=pOmVaINaL4mmYKIl5TXd00nvOdskpqyV6XngIDvr1BWXJC2Qe5xyKQ-MItZHhMfJLyBWBno8NqgEAdlSzu56IG01HuqCzm9F5anA4LFdXsJJyKuzvOteSDNqnVocGIaaQbGOxNuOLLg8UfuBE56RJwaVEjyXdOKumCesOsg0o_zVLDgdeNakOjVC1DUCuZxk833qtkW78FaHLkZq-1Hvwb9UdQ6ctj7evmAhDRktOCEoEIiRHOELpWUd91p9konk32Y2kySRACgWPzL6yywFcaBRVNZVYx8Gwkdxu-EyPcD9_XRGUCPSP6sq8CdS55Dr2q2NuKKipk66AqF8C1e4rw&mv=8011015.com.android.vending&bas_off=0&format=320x50_mb&oar=0&net=wi&app_name=4.android.imamalsajadsayings.android.com&hl=en&gnt=0&u_h=432&bas_on=0&ptime=0&u_audio=1&imbf=8009&u_so=l&output=html&region=mobile_app&u_tz=180&client_sdk=1&ex=1&slotname=a152b5665edfefd&kw_type=broad&gsb=wi&caps=inlineVideo_interactiveVideo_mraid1_th_autoplay_mediation_sdkAdmobApiForAds_di&jsv=66&urll=914
12-28 22:19:37.732: I/Ads(5571): Request scenario: Online server request.
12-28 22:19:37.962: D/OpenGLRenderer(5571): Flushing caches (mode 0)
12-28 22:19:38.862: D/ViewRootImpl(5571): pckname = imamalsajadsayings.android.com
12-28 22:19:38.992: D/OpenGLRenderer(5571): Flushing caches (mode 0)
12-28 22:19:40.142: D/AndroidRuntime(5571): Shutting down VM
12-28 22:19:40.142: W/dalvikvm(5571): threadid=1: thread exiting with uncaught exception (group=0x409d61f8)
12-28 22:19:40.152: E/AndroidRuntime(5571): FATAL EXCEPTION: main
12-28 22:19:40.152: E/AndroidRuntime(5571): java.lang.NullPointerException
12-28 22:19:40.152: E/AndroidRuntime(5571):     at imamalsajadsayings.android.com.SecondActivity$2.onClick(SecondActivity.java:46)
12-28 22:19:40.152: E/AndroidRuntime(5571):     at android.view.View.performClick(View.java:3511)
12-28 22:19:40.152: E/AndroidRuntime(5571):     at android.view.View$PerformClick.run(View.java:14109)
12-28 22:19:40.152: E/AndroidRuntime(5571):     at android.os.Handler.handleCallback(Handler.java:605)
12-28 22:19:40.152: E/AndroidRuntime(5571):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-28 22:19:40.152: E/AndroidRuntime(5571):     at android.os.Looper.loop(Looper.java:137)
12-28 22:19:40.152: E/AndroidRuntime(5571):     at android.app.ActivityThread.main(ActivityThread.java:4424)
12-28 22:19:40.152: E/AndroidRuntime(5571):     at java.lang.reflect.Method.invokeNative(Native Method)
12-28 22:19:40.152: E/AndroidRuntime(5571):     at java.lang.reflect.Method.invoke(Method.java:511)
12-28 22:19:40.152: E/AndroidRuntime(5571):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-28 22:19:40.152: E/AndroidRuntime(5571):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-28 22:19:40.152: E/AndroidRuntime(5571):     at dalvik.system.NativeStart.main(Native Method)
12-28 22:19:40.682: I/dalvikvm(5571): threadid=3: reacting to signal 3
12-28 22:19:40.682: I/dalvikvm(5571): Wrote stack traces to '/data/anr/traces.txt'
12-28 22:19:41.202: I/dalvikvm(5571): threadid=3: reacting to signal 3
12-28 22:19:41.202: I/dalvikvm(5571): Wrote stack traces to '/data/anr/traces.txt'
12-28 22:19:41.362: I/Process(5571): Sending signal. PID: 5571 SIG: 9
12-28 22:21:31.312: D/dalvikvm(5651): GC_CONCURRENT freed 180K, 4% free 6832K/7111K, paused 2ms+3ms
12-28 22:21:31.332: E/ActivityThread(5651): Failed to find provider info for com.google.plus.platform
12-28 22:21:31.362: I/Ads(5651): To get test ads on this device, call adRequest.addTestDevice("EC7F3FD189C6080DC9515A642B6941D1");
12-28 22:21:31.382: I/dalvikvm(5651): threadid=3: reacting to signal 3
12-28 22:21:31.402: I/dalvikvm(5651): Wrote stack traces to '/data/anr/traces.txt'
12-28 22:21:31.452: D/dalvikvm(5651): DexOpt: --- BEGIN 'ads804718022.jar' (bootstrap=0) ---
12-28 22:21:31.552: D/ViewRootImpl(5651): pckname = imamalsajadsayings.android.com
12-28 22:21:31.792: D/libEGL(5651): loaded /system/lib/egl/libEGL_mali.so
12-28 22:21:31.802: D/libEGL(5651): loaded /system/lib/egl/libGLESv1_CM_mali.so
12-28 22:21:31.822: D/libEGL(5651): loaded /system/lib/egl/libGLESv2_mali.so
12-28 22:21:31.852: D/OpenGLRenderer(5651): Enabling debug mode 0
12-28 22:21:31.882: I/dalvikvm(5651): threadid=3: reacting to signal 3
12-28 22:21:31.892: I/dalvikvm(5651): Wrote stack traces to '/data/anr/traces.txt'
12-28 22:21:34.432: D/dalvikvm(5651): GC_CONCURRENT freed 238K, 5% free 7084K/7431K, paused 3ms+4ms
12-28 22:21:34.472: D/dalvikvm(5651): GC_FOR_ALLOC freed 2K, 5% free 7081K/7431K, paused 31ms
12-28 22:21:34.472: I/dalvikvm-heap(5651): Grow heap (frag case) to 7.586MB for 614416-byte allocation
12-28 22:21:34.522: D/dalvikvm(5651): GC_FOR_ALLOC freed <1K, 5% free 7681K/8071K, paused 27ms
12-28 22:21:34.592: D/ViewRootImpl(5651): pckname = imamalsajadsayings.android.com
12-28 22:21:34.612: D/webviewglue(5651): nativeDestroy view: 0x163fab8
12-28 22:21:34.762: D/OpenGLRenderer(5651): Flushing caches (mode 0)
12-28 22:21:35.732: D/AndroidRuntime(5651): Shutting down VM
12-28 22:21:35.732: W/dalvikvm(5651): threadid=1: thread exiting with uncaught exception (group=0x409d61f8)
12-28 22:21:35.742: E/AndroidRuntime(5651): FATAL EXCEPTION: main
12-28 22:21:35.742: E/AndroidRuntime(5651): java.lang.NullPointerException
12-28 22:21:35.742: E/AndroidRuntime(5651):     at imamalsajadsayings.android.com.SecondActivity$2.onClick(SecondActivity.java:49)
12-28 22:21:35.742: E/AndroidRuntime(5651):     at android.view.View.performClick(View.java:3511)
12-28 22:21:35.742: E/AndroidRuntime(5651):     at android.view.View$PerformClick.run(View.java:14109)
12-28 22:21:35.742: E/AndroidRuntime(5651):     at android.os.Handler.handleCallback(Handler.java:605)
12-28 22:21:35.742: E/AndroidRuntime(5651):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-28 22:21:35.742: E/AndroidRuntime(5651):     at android.os.Looper.loop(Looper.java:137)
12-28 22:21:35.742: E/AndroidRuntime(5651):     at android.app.ActivityThread.main(ActivityThread.java:4424)
12-28 22:21:35.742: E/AndroidRuntime(5651):     at java.lang.reflect.Method.invokeNative(Native Method)
12-28 22:21:35.742: E/AndroidRuntime(5651):     at java.lang.reflect.Method.invoke(Method.java:511)
12-28 22:21:35.742: E/AndroidRuntime(5651):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-28 22:21:35.742: E/AndroidRuntime(5651):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-28 22:21:35.742: E/AndroidRuntime(5651):     at dalvik.system.NativeStart.main(Native Method)
12-28 22:21:36.282: I/dalvikvm(5651): threadid=3: reacting to signal 3
12-28 22:21:36.292: I/dalvikvm(5651): Wrote stack traces to '/data/anr/traces.txt'
12-28 22:21:36.802: I/dalvikvm(5651): threadid=3: reacting to signal 3
12-28 22:21:36.822: I/dalvikvm(5651): Wrote stack traces to '/data/anr/traces.txt'
12-28 22:21:37.122: I/Process(5651): Sending signal. PID: 5651 SIG: 9

我希望你可以帮我修复副本到剪贴板代码,只要按下按钮,按钮就会将textview“两个”复制,感谢

4 个答案:

答案 0 :(得分:1)

您尚未实例化TextView 两个。因此,当您在OnClick of按钮(使用ClipBoard的地方)中调用NullPointerException时,您将获得two.getText().toString

实例化onCreate()

two = (TextView)findViewById(R.id.idOfTextView);

答案 1 :(得分:0)

在onCreate()

中添加
two= (TextView) findViewById(R.id.textview);

答案 2 :(得分:0)

你有重复的按钮:

button = (Button) findViewById(R.id.button1);

button = (Button) findViewById(R.id.button2);

答案 3 :(得分:-1)

不应该为r.id.button1和r.id.button2实例化两个按钮!你也有两个onclicklisteners设置相同的按钮!当然,edittext没有像其他回答者所指出的那样初始化!