java - 使用按钮开始 Activity

标签 java android eclipse android-activity

我已经浏览了这个站点几个小时,但没有找到解决我的问题的方法: Eclipse 告诉我这个没有错误,但是当我按下“主屏幕”的"is"(打开下一个 Activity 的那个)按钮时,应用程序崩溃了 list :

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Openscreen"
        android:label="@string/title_activity_openscreen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Q1"
        android:label="@string/title_activity_q1" >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Q2"
        android:label="@string/title_activity_q2" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>
</application>

</manifest>

这是我的 Openscreen.java 文件:

package com.atf.the.ultimate.onehundred.quiz;

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

public class Openscreen extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_openscreen);

    Button homescreen_yes = (Button) findViewById(R.id.homescreen_yes);
    homescreen_yes.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
        Intent homescreen_yes = new Intent(Openscreen.this, Q1.class);
        startActivity(homescreen_yes);
        }
    });
    }

 public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_openscreen, menu);
    return true;
}

这是我的 openscreen(activity_openscreen.xml) 的 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" >

 <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="68dp"
    android:text="Hello and welcome to the 100 questions quiz. In this quiz you will be     asked a series of quiestions, each with 3 answer possibilities. Select the right one to   proceed, if you fail a quiestion you will lose and have to start over again! Remember to save when you exit, progress is not saved automatically!"
    tools:context=".Openscreen" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Are you ready to start?"
    tools:context=".Openscreen" />

<Button
    android:id="@+id/homescreen_yes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="78dp"
    android:text="Yes!"
    android:onClick= "" />

<Button
    android:id="@+id/homescreen_no"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/homescreen_yes"
    android:layout_alignBottom="@+id/homescreen_yes"
    android:layout_alignRight="@+id/textView2"
    android:text="No!"
    android:onClick="exit" />

</RelativeLayout>

日志:

     10-21 00:25:29.972: INFO/ActivityManager(67): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.atf.the.ultimate.onehundred.quiz/.Openscreen } from pid 139
10-21 00:25:31.110: INFO/ActivityManager(67): Start proc com.atf.the.ultimate.onehundred.quiz for activity com.atf.the.ultimate.onehundred.quiz/.Openscreen: pid=813 uid=10034 gids={}
10-21 00:25:34.120: INFO/ActivityManager(67): Displayed com.atf.the.ultimate.onehundred.quiz/.Openscreen: +3s399ms (total +30m34s364ms)
10-21 00:25:36.670: INFO/ActivityManager(67): Starting: Intent { cmp=com.atf.the.ultimate.onehundred.quiz/.Q1 } from pid 813
10-21 00:25:37.070: DEBUG/AndroidRuntime(813): Shutting down VM
10-21 00:25:37.080: WARN/dalvikvm(813): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-21 00:25:37.131: ERROR/AndroidRuntime(813): FATAL EXCEPTION: main
10-21 00:25:37.131: ERROR/AndroidRuntime(813): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.atf.the.ultimate.onehundred.quiz/com.atf.the.ultimate.onehundred.quiz.Q1}: java.lang.NullPointerException
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at android.os.Looper.loop(Looper.java:123)
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at java.lang.reflect.Method.invokeNative(Native Method)
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at java.lang.reflect.Method.invoke(Method.java:507)
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at dalvik.system.NativeStart.main(Native Method)
10-21 00:25:37.131: ERROR/AndroidRuntime(813): Caused by: java.lang.NullPointerException
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at com.atf.the.ultimate.onehundred.quiz.Q1.onCreate(Q1.java:19)
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-21 00:25:37.131: ERROR/AndroidRuntime(813):     ... 11 more
10-21 00:25:37.180: WARN/ActivityManager(67):   Force finishing activity com.atf.the.ultimate.onehundred.quiz/.Q1
10-21 00:25:37.283: WARN/ActivityManager(67):   Force finishing activity com.atf.the.ultimate.onehundred.quiz/.Openscreen
10-21 00:25:37.790: WARN/ActivityManager(67): Activity pause timeout for HistoryRecord{40803998 com.atf.the.ultimate.onehundred.quiz/.Q1}
10-21 00:25:40.120: INFO/Process(813): Sending signal. PID: 813 SIG: 9
10-21 00:25:40.231: INFO/ActivityManager(67): Process com.atf.the.ultimate.onehundred.quiz (pid 813) has died.
10-21 00:25:40.240: ERROR/InputDispatcher(67): channel '4075d390 com.atf.the.ultimate.onehundred.quiz/com.atf.the.ultimate.onehundred.quiz.Openscreen (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
10-21 00:25:40.250: ERROR/InputDispatcher(67): channel '4075d390 com.atf.the.ultimate.onehundred.quiz/com.atf.the.ultimate.onehundred.quiz.Openscreen (server)' ~ Channel is unrecoverably broken and will be disposed!
10-21 00:25:40.510: INFO/WindowManager(67): WIN DEATH: Window{4075d390 com.atf.the.ultimate.onehundred.quiz/com.atf.the.ultimate.onehundred.quiz.Openscreen paused=true}
10-21 00:25:40.850: WARN/InputManagerService(67): Got RemoteException sending setActive(false) notification to pid 813 uid 10034
10-21 00:25:49.770: WARN/ActivityManager(67): Activity destroy timeout for HistoryRecord{40811148 com.atf.the.ultimate.onehundred.quiz/.Openscreen}
10-21 00:25:49.920: WARN/ActivityManager(67): Activity destroy timeout for HistoryRecord{40803998 com.atf.the.ultimate.onehundred.quiz/.Q1}
10-21 00:25:53.320: DEBUG/SntpClient(67): request time failed: java.net.SocketException:     Address family not supported by protocol

评论按钮不起作用;但它还是崩溃了

最佳答案

从按钮中删除 android:onClick,因为您没有使用该方法调用它们。

编辑: 您的 Log-Cat 显示 Q1 类中存在 NullPointerException

关于java - 使用按钮开始 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12993806/

相关文章:

java - 创建一个 Ant 目标来运行 jar 中的 java 类

C# Xamarin 安卓 "Central Directory record could not be found"

php - Aptana 拼写检查

java - Janino动态编译接口(interface)类

Java 代码在逐步 Debug模式下运行良好,而不是在默认运行模式下运行良好

android - 如何在android中的方向期间保存选项卡状态

Android:未找到 Firebase 导入

android - 如何更改android中的工作区?

java - 将 Eclipse 项目重新连接到新的 SVN 存储库

java - StringTokenizer NoSuchElements