English 中文(简体)
我不敢开始活动?
原标题:My app is unable to start activity?

When i try to run the app on my phone,it force closes when i try to do activity 3. Logcat says:

01-13 17:53:25.368: E/AndroidRuntime(3235): Caused by: java.lang.NullPointerException 01-13 17:53:25.368: E/AndroidRuntime(3235): at android.app.activity3.onCreate(activity3.java:18)

第18条

Button wg = (Button) findViewById(R.id.Back); 

我的《活动守则》3.java:

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


public class activity3 extends Activity{

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main3);

        Button wg = (Button) findViewById(R.id.Back);
        wg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }

        });
    }
}

预 收

最佳回答

你需要修改你的Xml:

<?xml version="1.0" encoding="utf-8"?>

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

    <!-- your textview -->
    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/text1"
        android:textAppearance="?android:attr/textAppearanceMedium"
        />

    <!-- your back button -->
    <Button
        android:id="@+id/back"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/Back"
        />

</RelativeLayout>

我做了些什么:

  • Removed text and orientation from your root RelativeLayout, these do nothing.
  • Modified your height and width, feel free to change these as you please.
  • Added a Button with id of R.id.back and text of @string/Back.

现在,你能够利用发现ById(R.id.back)来查阅你的子州,并点击听众。

问题回答

我在xml中不见一顿。 并且,一旦有这样的情况,它就需要:

android:id="@+id/Back"

之所以出现这种情况,是因为你看不到一个纽子。 只有一份案文概览。 你们必须建立托邦。 你们只字不提,因此就没有。

你们可以找到一个 n子:here

你没有用贴上Xml的“Back”的ton子,这就是为什么你在那里拿走。 页: 1





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签