我在我的主要活动上做了一个按钮,该按钮应该打开另一个活动。我在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>
答案 0 :(得分:0)
转到构建并按清洁项目。 (构建 - &gt;清洁项目) 这有助于您解决无法恢复符号'R'错误
答案 1 :(得分:0)
使用
import com.example.cristian.befficient.R;
鉴于com.example.cristian.befficient是应用程序的包。
答案 2 :(得分:0)
在 NewAct 行 12
中使用445653
代替supportActionBar
答案 3 :(得分:0)
操作栏错误
getSupportActionBar()
代替getActionBar()
public class NewAct extends AppCompatActivity
答案 4 :(得分:0)
无论出于何种原因,您的操作栏可能为空。检查主要活动的样式,并确保已启用操作栏。
检查你的 - values / styles.xml并确保它有一个操作栏。
<style name = "AppTheme" parent = "android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
请查看此链接以获取更多信息https://developer.android.com/design/patterns/actionbar.html