我已经链接了许多活动,以构建一个应用程序,但是最初的一个,现在当我开始时,在使用时会出错。尽管该活动最初工作,但现在,它显示应用程序已强制关闭。任何人都可以帮我解决这个问题吗?另外,如何附加logcat,请告诉我。我希望看到这可能会帮助你们所有人,更好地解决问题。 谢谢!
package com.example.newapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add, sub, change, mail, camera;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
display=(TextView) findViewById(R.id.textView1);
change = (Button) findViewById(R.id.change);
mail = (Button) findViewById(R.id.mail);
camera = (Button) findViewById(R.id.camera);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter = counter + 1;
display.setText("Answer is "+ counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter = counter - 1;
display.setText("Answer is "+ counter);
}
});
change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startTextPlay = new Intent("com.example.newapplication.TEXTPLAY");
startActivity(startTextPlay);
}
});
mail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent startMailer = new Intent("com.example.newapplication.EMAIL");
startActivity(startMailer);
}
});
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent startCamera = new Intent("com.example.newapplication.CAMERA");
startActivity(startCamera);
}
});
}
}
这是主要活动中的代码,活动的代码没有启动,下面再次给出了更改。
package com.example.newapplication;
import java.util.Random;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;
public class TextPlay extends Activity{
Button chkCommand = (Button) findViewById(R.id.b1);
ToggleButton passTog = (ToggleButton) findViewById(R.id.tB1);
EditText input = (EditText) findViewById(R.id.etCommands);
TextView display = (TextView) findViewById(R.id.tv);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
passTog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (passTog.isChecked()){
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
} else{
input.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
});
chkCommand.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String message = input.getText().toString();
if(message.contentEquals("naman")){
display.setTextColor(Color.GREEN);
display.setGravity(Gravity.CENTER);
display.setText("Password is correct");
Random crazy = new Random();
display.setTextSize(crazy.nextInt(75));
display.setTextColor(Color.rgb(crazy.nextInt(200), crazy.nextInt(256), crazy.nextInt(256)));
}
else{
display.setTextColor(Color.RED);
display.setGravity(Gravity.CENTER);
display.setText("Invalid Password");
}
}
});
}
}
logcat如下所示:
05-29 01:52:40.280: E/AndroidRuntime(892): FATAL EXCEPTION: main
05-29 01:52:40.280: E/AndroidRuntime(892): Process: com.example.newapplication, PID: 892
05-29 01:52:40.280: E/AndroidRuntime(892): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.newapplication/com.example.newapplication.TextPlay}: java.lang.NullPointerException
05-29 01:52:40.280: E/AndroidRuntime(892): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
05-29 01:52:40.280: E/AndroidRuntime(892): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-29 01:52:40.280: E/AndroidRuntime(892): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-29 01:52:40.280: E/AndroidRuntime(892): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-29 01:52:40.280: E/AndroidRuntime(892): at android.os.Handler.dispatchMessage(Handler.java:102)
05-29 01:52:40.280: E/AndroidRuntime(892): at android.os.Looper.loop(Looper.java:136)
05-29 01:52:40.280: E/AndroidRuntime(892): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-29 01:52:40.280: E/AndroidRuntime(892): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 01:52:40.280: E/AndroidRuntime(892): at java.lang.reflect.Method.invoke(Method.java:515)
05-29 01:52:40.280: E/AndroidRuntime(892): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-29 01:52:40.280: E/AndroidRuntime(892): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-29 01:52:40.280: E/AndroidRuntime(892): at dalvik.system.NativeStart.main(Native Method)
05-29 01:52:40.280: E/AndroidRuntime(892): Caused by: java.lang.NullPointerException
05-29 01:52:40.280: E/AndroidRuntime(892): at android.app.Activity.findViewById(Activity.java:1884)
05-29 01:52:40.280: E/AndroidRuntime(892): at com.example.newapplication.TextPlay.<init>(TextPlay.java:18)
05-29 01:52:40.280: E/AndroidRuntime(892): at java.lang.Class.newInstanceImpl(Native Method)
05-29 01:52:40.280: E/AndroidRuntime(892): at java.lang.Class.newInstance(Class.java:1208)
05-29 01:52:40.280: E/AndroidRuntime(892): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-29 01:52:40.280: E/AndroidRuntime(892): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
05-29 01:52:40.280: E/AndroidRuntime(892): ... 11 more
Android Manifest文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newapplication"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<permission android:name="android.permission.SET_WALLPAPER"></permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.newapplication.Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.newapplication.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.newapplication.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.newapplication.TextPlay"
android:label="@string/app_name"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="com.example.newapplication.TEXTPLAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.newapplication.Email"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.newapplication.EMAIL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.newapplication.Camera"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.newapplication.CAMERA" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:3)
移动此代码TextPlay.java
Button chkCommand = (Button) findViewById(R.id.b1);
ToggleButton passTog = (ToggleButton) findViewById(R.id.tB1);
EditText input = (EditText) findViewById(R.id.etCommands);
TextView display = (TextView) findViewById(R.id.tv);
之后
setContentView(R.layout.text);
在设置xml文件的内容之前尝试查找视图时,您将收到Null Pointer Exception。希望这会有所帮助。
答案 1 :(得分:0)
此代码:
Button chkCommand = (Button) findViewById(R.id.b1);
ToggleButton passTog = (ToggleButton) findViewById(R.id.tB1);
EditText input = (EditText) findViewById(R.id.etCommands);
TextView display = (TextView) findViewById(R.id.tv);
它应该在OnCreate方法内部并且在setContentView(R.layout.text);
下面这一行在类中你可以声明变量初始化在OnCreate方法中完成。
public class TextPlay extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
Button chkCommand = (Button) findViewById(R.id.b1);
ToggleButton passTog = (ToggleButton) findViewById(R.id.tB1);
EditText input = (EditText) findViewById(R.id.etCommands);
TextView display = (TextView) findViewById(R.id.tv);
passTog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (passTog.isChecked()){
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
} else{
input.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
});
chkCommand.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String message = input.getText().toString();
if(message.contentEquals("naman")){
display.setTextColor(Color.GREEN);
display.setGravity(Gravity.CENTER);
display.setText("Password is correct");
Random crazy = new Random();
display.setTextSize(crazy.nextInt(75));
display.setTextColor(Color.rgb(crazy.nextInt(200), crazy.nextInt(256), crazy.nextInt(256)));
}
else{
display.setTextColor(Color.RED);
display.setGravity(Gravity.CENTER);
display.setText("Invalid Password");
}
}
});
}
}