简体   繁体   中英

unfortunately, <your app> has stopped while i am trying it run it in my android emulator. how to solve this?

i am new to Android development and i encounter a problem that the app itself unfortunately stopped but it has no error..

here is my code.

MainActivity.java
package com.mobilebasedsignlanguage;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {    
    Button conv = (Button)findViewById(R.id.btn_conv);
Button inst = (Button)findViewById(R.id.btn_ins);
Button view = (Button)findViewById(R.id.btn_view);
Button ext = (Button)findViewById(R.id.btn_ex);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    conv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent in = new Intent(MainActivity.this, viewmenu.class);
            startActivity(in);
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

activity_main.xml

<RelativeLayout 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: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=".MainActivity">

<Button
    android:id="@+id/btn_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btn_ex"
    android:layout_alignLeft="@+id/btn_conv"
    android:layout_alignRight="@+id/btn_conv"
    android:layout_marginBottom="60dp"
    android:text="View" />

<Button
    android:id="@+id/btn_conv"
    android:layout_width="250sp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btn_ins"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="44dp"
    android:text="Convert" />

<Button
    android:id="@+id/btn_ex"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btn_view"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/btn_view"
    android:layout_marginBottom="112dp"
    android:text="Exit" />

<Button
    android:id="@+id/btn_ins"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btn_view"
    android:layout_alignLeft="@+id/btn_view"
    android:layout_alignRight="@+id/btn_conv"
    android:layout_marginBottom="46dp"
    android:text="Instruction" />

</RelativeLayout>

here is the second screen.

viewmenu.java

package com.mobilebasedsignlanguage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class viewmenu extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewlayout);

    Button alphaconv = (Button)findViewById(R.id.bt_Alphabet);

    alphaconv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent alpha = new Intent(viewmenu.this, alphaconv.class);
            startActivity(alpha);
        }
    });

}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobilebasedsignlanguage"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.mobilebasedsignlanguage.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="com.mobilebasedsignlanguage.viewmenu"></activity>

</application>

</manifest>

Here's the Log Cat

02-18 10:23:32.314: E/Trace(2192): error opening trace file: No such file or directory (2)
02-18 10:23:32.695: D/AndroidRuntime(2192): Shutting down VM
02-18 10:23:32.695: W/dalvikvm(2192): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
02-18 10:23:32.794: E/AndroidRuntime(2192): FATAL EXCEPTION: main
02-18 10:23:32.794: E/AndroidRuntime(2192): java.lang.RuntimeException: Unable to     instantiate activity     ComponentInfo{com.mobilebasedsignlanguage/com.mobilebasedsignlanguage.MainActivity}:     java.lang.NullPointerException
02-18 10:23:32.794: E/AndroidRuntime(2192):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at android.os.Looper.loop(Looper.java:137)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at java.lang.reflect.Method.invoke(Method.java:511)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at dalvik.system.NativeStart.main(Native Method)
02-18 10:23:32.794: E/AndroidRuntime(2192): Caused by: java.lang.NullPointerException
02-18 10:23:32.794: E/AndroidRuntime(2192):     at     android.app.Activity.findViewById(Activity.java:1839)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at com.mobilebasedsignlanguage.MainActivity.<init>(MainActivity.java:12)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at java.lang.Class.newInstanceImpl(Native Method)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at java.lang.Class.newInstance(Class.java:1319)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
02-18 10:23:32.794: E/AndroidRuntime(2192):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
02-18 10:23:32.794: E/AndroidRuntime(2192):     ... 11 more

In your MainActivty.java class you need to declare this :

super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);

Instantiate Buttons in OnCreate() below setContentView()

Hope this helps.

Move these lines

Button conv = (Button)findViewById(R.id.btn_conv);
Button inst = (Button)findViewById(R.id.btn_ins);
Button view = (Button)findViewById(R.id.btn_view);
Button ext = (Button)findViewById(R.id.btn_ex);

to onCreate() of Activity after setContentView() call...

You need to initialize views once you set the content of the layout to the activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main)  // this must be first
      // then initialize views
      Button conv = (Button)findViewById(R.id.btn_conv);
      Button inst = (Button)findViewById(R.id.btn_ins);
      Button view = (Button)findViewById(R.id.btn_view);
      Button ext = (Button)findViewById(R.id.btn_ex);

findViewById looks for a view with the id in the current inflated layout.

Also rename viewmenu to ViewMenu and follow java naming conventions

Do the declaration and initialization as given below...

Button conv;
Button inst;
Button view;
Button ext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    conv = (Button)findViewById(R.id.btn_conv);
    inst = (Button)findViewById(R.id.btn_ins);
    view = (Button)findViewById(R.id.btn_view);
    ext = (Button)findViewById(R.id.btn_ex);
}

Since you are initializing them before your main view creation that's why those could not be find.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM