单击按钮时活动没有开始?

时间:2013-02-23 08:51:45

标签: android android-activity

  Button b1=(Button)findViewById(R.id.b1);
           final EditText ed=(EditText)findViewById(R.id.ed1);
           TextView t=(TextView)findViewById(R.id.t1);
           b1.setOnClickListener(new Button.OnClickListener() { 
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                     abc=ed.getText().toString();
                    Intent intent=new Intent(v.getContext(),AnotherActivity.class);
                    startActivityForResult(intent,0);


                }
                 })

这是我的主要活动代码我正在开始另一个活动onbutton click而不是我收到错误而我的应用程序崩溃可以有人帮我删除此错误/

02-23 14:20:06.557: E/AndroidRuntime(2732): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newpicker/com.example.newpicker.AnotherActivity}: java.lang.NullPointerException

这是我的logcat

public class AnotherActivity extends Activity {

 static Context context;
   public boolean a;
    private DownloadManager downloadManager;
 static TextView tt1;
  String url="http://www.digitalmarketingbox.com/";
  private DownloadManager dm;
  String v1,v2;
   ArrayList<String> imageid = new ArrayList<String>();
    ArrayList<String> imagepath = new ArrayList<String>();
   ArrayList<String> playlist_content = new ArrayList<String>();
     ArrayList<String> al=new ArrayList();
     String i1;
   @Override
   protected void onCreate(Bundle savedInstanceState) 
   {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.showimages);
            //tt1=(TextView)findViewById(R.id.text1);
            TextView tt1=(TextView)findViewById(R.id.it);
            AnotherActivity.tt1.append(MainActivity.abc);

   }

    02-23 14:35:08.561: D/dalvikvm(4900): Late-enabling CheckJNI
02-23 14:35:08.702: D/libEGL(4900): loaded /system/lib/egl/libEGL_adreno200.so
02-23 14:35:08.702: D/libEGL(4900): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
02-23 14:35:08.702: D/libEGL(4900): loaded /system/lib/egl/libGLESv2_adreno200.so
02-23 14:35:08.702: I/Adreno200-EGL(4900): <eglInitialize:269>: EGL 1.4 QUALCOMM build: Nondeterministic AU_full_mako_PARTNER-ANDROID/JB-MR1-DEV_CL2961380_release_AU (CL2961380)
02-23 14:35:08.702: I/Adreno200-EGL(4900): Build Date: 12/10/12 Mon
02-23 14:35:08.702: I/Adreno200-EGL(4900): Local Branch: 
02-23 14:35:08.702: I/Adreno200-EGL(4900): Remote Branch: m/partner-android/jb-mr1-dev
02-23 14:35:08.702: I/Adreno200-EGL(4900): Local Patches: NONE
02-23 14:35:08.702: I/Adreno200-EGL(4900): Reconstruct Branch: NOTHING
02-23 14:35:08.742: D/OpenGLRenderer(4900): Enabling debug mode 0
02-23 14:35:12.936: D/AndroidRuntime(4900): Shutting down VM
02-23 14:35:12.936: W/dalvikvm(4900): threadid=1: thread exiting with uncaught exception (group=0x41d10930)
02-23 14:35:12.946: E/AndroidRuntime(4900): FATAL EXCEPTION: main
02-23 14:35:12.946: E/AndroidRuntime(4900): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newpicker/com.example.newpicker.AnotherActivity}: java.lang.NullPointerException
02-23 14:35:12.946: E/AndroidRuntime(4900):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-23 14:35:12.946: E/AndroidRuntime(4900):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-23 14:35:12.946: E/AndroidRuntime(4900):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-23 14:35:12.946: E/AndroidRuntime(4900):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-23 14:35:12.946: E/AndroidRuntime(4900):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 14:35:12.946: E/AndroidRuntime(4900):     at android.os.Looper.loop(Looper.java:137)
02-23 14:35:12.946: E/AndroidRuntime(4900):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-23 14:35:12.946: E/AndroidRuntime(4900):     at java.lang.reflect.Method.invokeNative(Native Method)
02-23 14:35:12.946: E/AndroidRuntime(4900):     at java.lang.reflect.Method.invoke(Method.java:511)
02-23 14:35:12.946: E/AndroidRuntime(4900):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-23 14:35:12.946: E/AndroidRuntime(4900):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-23 14:35:12.946: E/AndroidRuntime(4900):     at dalvik.system.NativeStart.main(Native Method)
02-23 14:35:12.946: E/AndroidRuntime(4900): Caused by: java.lang.NullPointerException
02-23 14:35:12.946: E/AndroidRuntime(4900):     at com.example.newpicker.AnotherActivity.onCreate(AnotherActivity.java:41)
02-23 14:35:12.946: E/AndroidRuntime(4900):     at android.app.Activity.performCreate(Activity.java:5104)
02-23 14:35:12.946: E/AndroidRuntime(4900):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-23 14:35:12.946: E/AndroidRuntime(4900):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-23 14:35:12.946: E/AndroidRuntime(4900):     ... 11 more

这是我的完整logcat

4 个答案:

答案 0 :(得分:1)

我认为你需要改变这行代码:

b1.setOnClickListener(new Button.OnClickListener() {

进入这个:

b1.setOnClickListener(new View.OnClickListener() {

正如您在按钮功能中使用View作为参数来处理按钮事件侦听器。希望它会有所帮助。

答案 1 :(得分:1)

试试如下:

  b1.setOnClickListener(new View.OnClickListener() { 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 abc=ed.getText().toString();
                Intent intent=new Intent(this,AnotherActivity.class);
                startActivity(intent);
            }
             });

并确保您已在清单中定义AnotherActivity

答案 2 :(得分:1)

我已经将textview定义为静态上面并尝试再次将其定义为正常的,只是检查它并且问题解决了谢谢你们

public class AnotherActivity extends Activity {

  static Context context;
  public boolean a;
  private DownloadManager downloadManager;
  static TextView tt1;
  String url="http://www.digitalmarketingbox.com/";
  private DownloadManager dm;
  String v1,v2;
  ArrayList<String> imageid = new ArrayList<String>();
  ArrayList<String> imagepath = new ArrayList<String>();
  ArrayList<String> playlist_content = new ArrayList<String>();
  ArrayList<String> al=new ArrayList();
  String i1;
 @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showimages);
        tt1=(TextView)findViewById(R.id.it);
        //---tt1.append(MainActivity.abc);
  }

答案 3 :(得分:0)

昨天这件事发生在我身上。甚至我在Manifest内定义了活动,但我努力推出它。我做的是检查我是否在我的清单上写了类名。并检查它是否正常工作,我将其更改为MainActivity并且它有效,所以我知道出了问题。无论如何,我确实通过使用此

使其工作
                if(Build.MANUFACTURER.contains("Samsung") || Build.MANUFACTURER.contains("samsung")){
            doAll();
            }else {
                Intent intent = new Intent(Smaller.this.getApplicationContext(),ForOthers.class);
                startActivity(intent);
                finish();
              }

这是我在用户启动应用时检查的内容。忘记它,只看到

YourClassName.this.getApplicationContext(),OtherAcitvity.class