我是Android(和java)的初学者,我只是在玩,想在单击按钮后创建新视图。到目前为止,这是我的代码:
主班
package myTests.homeSpace;
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 TestsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener( new OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(TestsActivity.this, screen1.class);
startActivity(myIntent);
}
});
}
}
单击按钮后要运行的类
package myTests.homeSpace;
import android.app.Activity;
import android.os.Bundle;
public class screen1 extends Activity {
@Override
public void onCreate(Bundle SavedBundleInstance)
{
super.onCreate(SavedBundleInstance);
setContentView(R.layout.screen1);
}
}
问题是,点击按钮后,我得到“不幸的是,测试已停止”错误。(“测试”是应用程序名称)。我知道(或猜想)问题出在这一行Intent myIntent = new Intent(TestsActivity.this, screen1.class);
我猜我对类的引用在screen1
某种程度上是错误的,但我不知道为什么。没有编译错误也没有警告,布局 .xmls 不应该是错误的。
你们中的任何人都可以告诉我任何解决方案吗?
编辑
显现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myTests.homeSpace"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TestsActivity"
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>
控制台输出
[2012-06-20 19:56:21 - ddms] null
java.lang.NullPointerException
at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
at com.android.ddmlib.Client.sendAndConsume(Client.java:575)
at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
at com.android.ddmlib.Client.getJdwpPacket(Client.java:672)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)