on Click功能在我的应用程序中不起作用

时间:2016-09-30 16:23:20

标签: java android

我试图通过单击带有Intent类的按钮来显示另一个活动,但它仍然会出现错误,这是

  

“遗憾的是我的申请已停止”

我认为代码是正确的

这是“主要活动类

    course=(EditText) findViewById(R.id.editText);
    hour=(EditText) findViewById(R.id.editText1);
    add= (Button) findViewById(R.id.button);
    view_= (Button) findViewById(R.id.button1);

    view_.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(MainActivity.this,ViewActivity.class);
            startActivity(intent);
        }
    });

}
}

这是ViewActivity类

TextView tv;
Button ed, de, view_;


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

    tv = (TextView) findViewById(R.id.details);
    ed = (Button) findViewById(R.id.ed);
    de = (Button) findViewById(R.id.delete);
    view_= (Button) findViewById(R.id.button1);
    Intent intent = getIntent();

    view_.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}

另外,我在AndroidManifest.xml上添加了.ViewActivity

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

locat&gt;&gt;

FATAL EXCEPTION: main
                                                                        Process: com.example.android.intent, PID: 11316
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.intent/com.example.android.intent.ViewActivity}: java.lang.NullPointerException
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                                                                            at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:136)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:515)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                            at dalvik.system.NativeStart.main(Native Method)
                                                                         Caused by: java.lang.NullPointerException
                                                                            at com.example.android.intent.ViewActivity.onCreate(ViewActivity.java:34)
                                                                            at android.app.Activity.performCreate(Activity.java:5231)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                                                                            at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:136) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                                            at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                            at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                            at dalvik.system.NativeStart.main(Native Method) 

查看布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <TableRow
        android:layout_width="80dp"
        android:layout_height="50dp" >

        <TextView
            android:text="Course"
            android:layout_width="80dp"
            android:layout_height="50dp"
            android:id="@+id/course"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="10dp"
            android:textSize="24sp" />

        <TextView
            android:text="Hour"
            android:layout_width="80dp"
            android:layout_height="50dp"
            android:id="@+id/hour"
            android:layout_marginLeft="170dp"
            android:layout_marginTop="10dp"
            android:textSize="24sp"
            android:layout_marginRight="30dp" />



    </TableRow>

</TableLayout>

<Button
    android:text="Edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="45dp"
    android:id="@+id/ed"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="38dp"
    android:layout_marginStart="38dp"
    android:onClick="" />

<Button
    android:text="Delete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="19dp"
    android:layout_marginEnd="19dp"
    android:id="@+id/delete"
    android:layout_alignBaseline="@+id/ed"
    android:layout_alignBottom="@+id/ed"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="" />

<TextView
    android:layout_width="350dp"
    android:layout_height="350dp"
    android:id="@+id/details"
    android:layout_above="@+id/delete"
    android:layout_centerHorizontal="true" />

<Button
    android:text="Refresh"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/delete"
    android:layout_toLeftOf="@+id/delete"
    android:layout_toStartOf="@+id/delete"
    android:layout_marginRight="28dp"
    android:layout_marginEnd="28dp"
    android:id="@+id/button4"
    android:onClick="" />

任何人都可以提供帮助,谢谢:)

4 个答案:

答案 0 :(得分:1)

像这样更改您的清单,您在另一个活动中添加了ViewActivity。

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

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

将此添加到视图布局

<Button
    android:text="Close View Activity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button1" />

答案 1 :(得分:0)

您尚未关闭活动代码。将 AndroidManifest.xml 更改为:

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

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

答案 2 :(得分:0)

更改此

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

view_= (Button) findViewById(R.id.button4);

您的ViewActivity类的XML中没有Button button1,因此当您尝试访问button1时,会出现NullPointerException

答案 3 :(得分:-1)

在清单中尝试此操作。你把一个活动放在另一个活动中分开。

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

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