在android上启动新活动时获取异常

在android上启动新活动时获取异常,android,android-activity,Android,Android Activity,关于这一点,我读了很多书,但没有找到解决办法。 这是我的第一个android项目 我正在尝试从我的主要活动中打开一个活动。 我已经仔细检查了名称和配置文件,但是startactivity()过程失败了。 debbuger显示运行时错误。 正如u将c一样,startactivity被调用为onclick事件 我真的被困在这里,不知道还能做什么 非常感谢 *这是主要活动** package com.example.helloandroid; import android.app.Activity;

关于这一点,我读了很多书,但没有找到解决办法。 这是我的第一个android项目

我正在尝试从我的主要活动中打开一个活动。 我已经仔细检查了名称和配置文件,但是startactivity()过程失败了。 debbuger显示运行时错误。 正如u将c一样,startactivity被调用为onclick事件

我真的被困在这里,不知道还能做什么

非常感谢

*这是主要活动**

package com.example.helloandroid;
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 HelloworldActivity extends Activity {
    /** Called when the activity is first created. */  
    private OnClickListener gtScoringListener = new OnClickListener() {
        public void onClick(View v) { 
            Intent mintent = new Intent(HelloworldActivity.this ,imgScoring.class);
            startActivity(mintent);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(gtScoringListener);
        }
}
package com.example.helloandroid;

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;
import android.widget.TextView;

public class imgScoring extends Activity {
    private OnClickListener backBtnListener = new OnClickListener() {
        public void onClick(View v) { 
            Intent intent = new Intent(imgScoring.this, HelloworldActivity.class);
            startActivity(intent);
            }
    };
    private OnClickListener scoreBtnListener = new OnClickListener() {
        public void onClick(View v){
            TextView tv = (TextView)findViewById(R.id.editText1);
            tv.setText(((Button)v).getText());
            }
    };
    /** Called when the activity is first created. */  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.Button01);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.Button03);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.Button02);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.Button04);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.button3);
        button.setOnClickListener(backBtnListener);

        setContentView(R.layout.imgscoring);
        }
}
*这是目标活动:

package com.example.helloandroid;
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 HelloworldActivity extends Activity {
    /** Called when the activity is first created. */  
    private OnClickListener gtScoringListener = new OnClickListener() {
        public void onClick(View v) { 
            Intent mintent = new Intent(HelloworldActivity.this ,imgScoring.class);
            startActivity(mintent);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(gtScoringListener);
        }
}
package com.example.helloandroid;

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;
import android.widget.TextView;

public class imgScoring extends Activity {
    private OnClickListener backBtnListener = new OnClickListener() {
        public void onClick(View v) { 
            Intent intent = new Intent(imgScoring.this, HelloworldActivity.class);
            startActivity(intent);
            }
    };
    private OnClickListener scoreBtnListener = new OnClickListener() {
        public void onClick(View v){
            TextView tv = (TextView)findViewById(R.id.editText1);
            tv.setText(((Button)v).getText());
            }
    };
    /** Called when the activity is first created. */  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.Button01);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.Button03);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.Button02);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.Button04);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.button3);
        button.setOnClickListener(backBtnListener);

        setContentView(R.layout.imgscoring);
        }
}
这是androidmanifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.helloandroid"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="12" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".imgScoring"></activity>
        <activity android:name=".HelloworldActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

您应该发布stacktrace,但我假设错误在目标活动的这一行:

button.setOnClickListener(scoreBtnListener);
这将为您提供一个
NULL

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

因为您尚未在该活动中设置contentview,所以
findViewById()
无法返回任何内容。调用该
setOnclickLIstener()

时,您将得到一个空指针。您本应发布stacktrace,但我假设错误在目标活动的这一行:

button.setOnClickListener(scoreBtnListener);
这将为您提供一个
NULL

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

因为您尚未在该活动中设置contentview,所以
findViewById()
无法返回任何内容。调用该
setOnclickLIstener()

时,您将得到一个空指针。请尝试将类名从
imgScoring
更改为
imgScoring
。Java不允许类名以小写字母开头;-)


并放置
setContentView(R.layout.imgscoring)就在super.onCreate()之后

尝试将类名从
imgScoring
更改为
imgScoring
。Java不允许类名以小写字母开头;-)


并放置
setContentView(R.layout.imgscoring)就在super.onCreate()之后

@Uvs:to add to this-place
setContentView()
super.onCreate(savedInstanceState)
之后调用@Uvs:to add to this-place
setContentView()
super.onCreate(savedInstanceState)
之后调用。