我想给那个包含ImageView和TextView的相对布局选项,当它尝试打开它时,它会在模拟器上提供FC,即使我只将onClick提供给ImageView,即使我做了对于孩子来说可点击为假
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:weightSum="1"
android:background="@android:color/holo_red_light"
android:onClick="OpenHBDapp">
<ImageView
android:layout_width="88dp"
android:layout_height="110dp"
android:id="@+id/imageView2"
android:layout_weight="0.18"
android:src="@drawable/happy_birthday_cake"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingLeft="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/happy_birthday_apk"
android:id="@+id/textView5"
android:textSize="20sp"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/imageView2"
android:layout_toEndOf="@+id/imageView2"
android:paddingLeft="16dp" />
</RelativeLayout>
它是班级
package courses.omy.dasser.androidcousres;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class C1Lesson1 extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.c1_lesson1);
}
public void OpenHBDapp(View view) {
Intent intent = new Intent(this, HappyBirthday_app.class);
startActivity(intent);
}
}
清单
<activity
android:name=".HappyBirthday_app"
android:label="Happy Birthday">
</activity>
logcat的
02-02 13:02:50.418 23653-23653/courses.omy.dasser.androidcousres E/AndroidRuntime: FATAL EXCEPTION: main
Process: courses.omy.dasser.androidcousres, PID: 23653
java.lang.RuntimeException: Unable to start activity ComponentInfo{courses.omy.dasser.androidcousres/courses.omy.dasser.androidcousres.HappyBirthday_app}: 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 courses.omy.dasser.androidcousres.HappyBirthday_app.onCreate(HappyBirthday_app.java:17)
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 courses.omy.dasser.androidcousres;
import android.app.ActionBar;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
public class HappyBirthday_app extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.happy_birthday_app);
ActionBar actionBar = null;
actionBar.setTitle("Happy Birthday");
}
}
答案 0 :(得分:1)
更改
ActionBar actionBar = null;
到
ActionBar actionBar = getSupportActionBar();
可以更改为
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
将解决FC感谢
答案 1 :(得分:1)
您正在将null初始化为操作栏实例并在空对象上调用setTitle。这是应用程序崩溃的主要原因。我不知道你为什么这样做,但为了避免崩溃只是评论
ActionBar actionBar = null;
actionBar.setTitle("Happy Birthday");
行,它会起作用。
或按照@ankitagrawal
的建议更新您的代码希望这会对你有帮助!!!