我在尝试创建一个简单的应用程序时遇到了麻烦。根据您单击的按钮,我想加载内容填充的下一个活动,我希望这个小副标题栏的颜色更改为主页上按钮的颜色。我尝试使用一个名为webColor的变量来实现这一点,看看我是否可以先使用1个按钮才能使它成为所有这些变量。 Web链接是变量应该更改为的颜色代码,它看起来应该可以工作,但是当我运行它时,我得到以下内容。
05-18 23:07:08.573 1554-1554/com.dredaydesigns.dredaycreativeportfolio D/dalvikvm﹕ Late-enabling CheckJNI
05-18 23:07:08.917 1554-1554/com.dredaydesigns.dredaycreativeportfolio I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
05-18 23:07:08.917 1554-1554/com.dredaydesigns.dredaycreativeportfolio W/dalvikvm﹕ VFY: unable to resolve virtual method 406: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
05-18 23:07:08.953 1554-1554/com.dredaydesigns.dredaycreativeportfolio D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-18 23:07:08.953 1554-1554/com.dredaydesigns.dredaycreativeportfolio I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
05-18 23:07:08.953 1554-1554/com.dredaydesigns.dredaycreativeportfolio W/dalvikvm﹕ VFY: unable to resolve virtual method 428: Landroid/content/res/TypedArray;.getType (I)I
05-18 23:07:08.953 1554-1554/com.dredaydesigns.dredaycreativeportfolio D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-18 23:07:08.969 1554-1554/com.dredaydesigns.dredaycreativeportfolio D/AndroidRuntime﹕ Shutting down VM
05-18 23:07:08.969 1554-1554/com.dredaydesigns.dredaycreativeportfolio W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4ce1b20)
05-18 23:07:08.969 1554-1554/com.dredaydesigns.dredaycreativeportfolio E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.dredaydesigns.dredaycreativeportfolio, PID: 1554
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dredaydesigns.dredaycreativeportfolio/com.dredaydesigns.dredaycreativeportfolio.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.dredaydesigns.dredaycreativeportfolio.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
package com.dredaydesigns.dredaycreativeportfolio;
import android.R;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
String webColor;
Button webButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
webButton = (Button) findViewById(R.id.button1);
webButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String webColor = webButton.getText().toString();
Intent goToNext = new Intent(MainActivity.this,content_activity.class);
goToNext.putExtra(webColor,"#C41E72");
startActivity(goToNext);
}
});
}
答案 0 :(得分:0)
具体按照错误代码的说明查看您的代码:
Caused by: java.lang.NullPointerException
at com.dredaydesigns.dredaycreativeportfolio.MainActivity.onCreate(MainActivity.java:22)
如果仔细检查,您会看到错误在MainActivity.java line 22
。这条线是什么?检查它是否已经初始化。
答案 1 :(得分:0)
public class MainActivity extends ActionBarActivity {
String webColor;
Button webButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
webButton = (Button) findViewById(R.id.button1);
webButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String webColor = webButton.getText().toString();
Intent goToNext = new Intent(MainActivity.this,content_activity.class);
goToNext.putExtra(webColor,"#C41E72");
startActivity(goToNext);
}
});
}
替换它。
public class MainActivity extends **Activity** {
String webColor;
Button webButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
webButton = (Button) findViewById(R.id.button1);
webButton.setOnClickListener(new OnClickListener() {
@Override
**public void onClick(View v) {
webColor= webButton.getText().toString();
Intent goToNext = new Intent(MainActivity.this,SecondClassName.class);
goToNext.putExtra(webColor,"#C41E72");
startActivity(goToNext);
}
});
}
并确定您在清单中输入活动详情?
答案 2 :(得分:0)
setContentView(R.layout.activity_list_item);
webButton = (Button) findViewById(R.id.button1);
webButton.setOnClickListener
我认为,在布局文件" activity_list_item"中没有其id = button1的按钮,请确保使用正确的布局文件。第22行导致NullPointerException可能是" webButton.setOnClickListener",因为活动在这里找不到webButton。