我在我的主要活動上做了一個按鈕,應該打開另一個活動。我宣佈活動AndroidManifest.xml
和功能如下:當我按下按鈕打開一個新的活動時,應用程序崩潰
package com.example.cristian.befficient;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout.activity_main);
}
public void addact(View v)
{
startActivity(new Intent(MainActivity.this, NewAct.class));
}
的錯誤是一些有關的動作條,我該如何解決? 崩潰日誌:
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cristian.befficient, PID: 2299
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cristian.befficient/com.example.cristian.befficient.NewAct}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at com.example.cristian.befficient.NewAct.onCreate(NewAct.java:12)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
終止應用程序。
我想開的活動:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cristian.befficient.NewAct">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:text="@string/large_text" />
是的,它在清單文件
<activity
android:name=".NewAct"
android:label="@string/title_activity_new"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.cristian.befficient.MainActivity" />
</activity>
你有沒有加入你的第二個活動,你的表現?我們將需要崩潰日誌來幫助您 – Chol
[Android Studio將紅色標記爲R,並顯示錯誤消息「無法解析符號R」,但構建成功]的可能重複(http://stackoverflow.com/questions/17421104/ android-studio-marks -r-in-red-with-error-message-can-resolve-symbol-r-but) – ItamarG3
你的操作欄似乎爲空!你可以粘貼你的第二個活動 – Chol