2016-11-14 56 views
-2

我在我的主要活動上做了一個按鈕,應該打開另一個活動。我宣佈活動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

你有沒有加入你的第二個活動,你的表現?我們將需要崩潰日誌來幫助您 – Chol

+0

[Android Studio將紅色標記爲R,並顯示錯誤消息「無法解析符號R」,但構建成功]的可能重複(http://stackoverflow.com/questions/17421104/ android-studio-marks -r-in-red-with-error-message-can-resolve-symbol-r-but) – ItamarG3

+0

你的操作欄似乎爲空!你可以粘貼你的第二個活動 – Chol

回答

0

去蓋,按清潔poject被聲明。 (建設 - >清理項目) 這將幫助您解決不能resove符號「R」錯誤

+0

我仍然有一個錯誤,引起:java.lang.NullPointerException:嘗試調用虛擬方法'void android.app.ActionBar。setDisplayHomeAsUpEnabled(布爾)'空對象引用 在com.example.cristian.befficient.NewAct.onCreate(NewAct.java:12) –

0

使用

import com.example.cristian.befficient.R; 

因爲com.example.cristian.befficient是應用的應用包。

+0

我解決了錯誤與R,我有一個稱爲操作欄的東西錯誤 –

+0

清楚地該錯誤是由:java.lang.NullPointerException:試圖調用虛擬方法'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)'在null對象引用 在com.example.cristian.befficient.NewAct.onCreate(NewAct .java:12),這意味着你正在對空引用調用方法setDisplayHomeAsUpEnabled。你應該分享NewAct.java –

0

使用supportActionBar,而不是ActionBarNewAct

0

操作欄的錯誤

  • 使用getSupportActionBar()代替getActionBar()
  • public class NewAct extends AppCompatActivity
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

相關問題