2016-10-23 3 views
-2

Android Studio에서 초보자입니다. 내 휴대 전화에 내 응용 프로그램을 실행하려고하지만, 난 내 휴대 전화에 내 응용 프로그램을 실행하려고 할 때 불행하게도 응용 프로그램은 여기에 java.lang.RuntimeException : Android Studio에서 활동 ComponentInfo를 인스턴스화 할 수 없습니다.

을 중지되었습니다

표시 오류가 로그 캣 안드로이드 모니터에 보여입니다입니다 . 다음은

10-23 23:19:25.882 18014-18014/com.example.dhara.codesprint E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.dhara.codesprint, PID: 18014 
java.lang.RuntimeException: Unable to start activity ComponentInfo 

{com.example.dhara.codesprint/com.example.dhara.codesprint.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2299) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5372) 
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:970) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. 
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198) 
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130) 
at com.example.dhara.codesprint.MainActivity.onCreate(MainActivity.java:39) 
at android.app.Activity.performCreate(Activity.java:5258) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1099) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2239) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)  
at android.app.ActivityThread.access$800(ActivityThread.java:151)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:136)  
at android.app.ActivityThread.main(ActivityThread.java:5372)  
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:970)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)  
at dalvik.system.NativeStart.main(Native Method) 

package com.example.dhara.codesprint; 

import android.content.res.AssetManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity { 

    TextView text1; 
    TextView text2; 
    TextView text3; 
    TextView text4; 
    TextView text5; 
    TextView text6; 
    EditText text7; 
    Button checkAnswer; 
    Button reset; 

    //private WordDictionary dictionary; 
    private String currentWord; 
    private ArrayList<String> words; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     AssetManager assetManager = getAssets(); 
     try { 
      InputStream inputStream = assetManager.open("words.txt"); 
     } catch (IOException e) { 
      Toast toast = Toast.makeText(this, "Could not load dictionary", Toast.LENGTH_LONG); 
      toast.show(); 
     } 

     text1 = (TextView) findViewById(R.id.textView1); 
     text2 = (TextView) findViewById(R.id.textView2); 
     text3 = (TextView) findViewById(R.id.textView3); 
     text4 = (TextView) findViewById(R.id.textView4); 
     text5 = (TextView) findViewById(R.id.textView5); 
     text6 = (TextView) findViewById(R.id.textView6); 
     text7 = (EditText) findViewById(R.id.edittext); 
     checkAnswer = (Button) findViewById(R.id.button); 
     reset = (Button) findViewById(R.id.button2); 


     reset.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

      } 
     }); 



     checkAnswer.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

      } 
     }); 
    } 





} 

AndroidMainfest.xml MainActivity.java 코드입니다 :

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.dhara.codesprint"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="Codesprint" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:background="@color/background" 
    tools:context=".MainActivity"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/circle" 
     android:layout_marginLeft="13dp" 
     android:layout_marginStart="13dp" 
     android:layout_marginTop="14dp" 
     android:id="@+id/imageView1" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/circle" 
     android:id="@+id/imageView2" 
     android:layout_alignTop="@+id/imageView3" 
     android:layout_alignLeft="@+id/imageView5" 
     android:layout_alignStart="@+id/imageView5" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/circle" 
     android:id="@+id/imageView3" 
     android:layout_alignTop="@+id/imageView1" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginRight="24dp" 
     android:layout_marginEnd="24dp" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/circle" 
     android:layout_marginTop="27dp" 
     android:id="@+id/imageView4" 
     android:layout_below="@+id/imageView1" 
     android:layout_alignLeft="@+id/imageView1" 
     android:layout_alignStart="@+id/imageView1" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/circle" 
     android:id="@+id/imageView5" 
     android:layout_alignTop="@+id/imageView4" 
     android:layout_centerHorizontal="true" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/circle" 
     android:id="@+id/imageView6" 
     android:layout_alignTop="@+id/imageView5" 
     android:layout_alignLeft="@+id/imageView3" 
     android:layout_alignStart="@+id/imageView3" /> 

    <EditText 
     android:text="Your Answer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/imageView5" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="55dp" 
     android:id="@+id/edittext" 
     android:textSize="20sp" 
     android:textAlignment="center" 
     android:textStyle="normal|bold" 
     android:textColorLink="?attr/actionMenuTextColor" /> 

    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="31dp" 
     android:id="@+id/textView2" 
     android:textAlignment="center" 
     android:textStyle="normal|bold" 
     android:layout_alignTop="@+id/imageView2" 
     android:layout_alignLeft="@+id/imageView2" 
     android:layout_alignStart="@+id/imageView2" 
     android:layout_alignRight="@+id/imageView2" 
     android:layout_alignEnd="@+id/imageView2" 
     android:textColor="@android:color/background_light" /> 

    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/textView2" 
     android:layout_alignRight="@+id/imageView3" 
     android:layout_alignEnd="@+id/imageView3" 
     android:id="@+id/textView3" 
     android:layout_alignLeft="@+id/imageView3" 
     android:layout_alignStart="@+id/imageView3" 
     android:textAlignment="center" 
     android:textStyle="normal|bold" 
     android:textColor="@android:color/background_light" /> 

    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/imageView4" 
     android:layout_alignRight="@+id/imageView4" 
     android:layout_alignEnd="@+id/imageView4" 
     android:layout_marginTop="30dp" 
     android:id="@+id/textView4" 
     android:layout_alignLeft="@+id/imageView4" 
     android:layout_alignStart="@+id/imageView4" 
     android:textAlignment="center" 
     android:textStyle="normal|bold" 
     android:textColor="@android:color/background_light" /> 

    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/textView4" 
     android:layout_alignRight="@+id/imageView5" 
     android:layout_alignEnd="@+id/imageView5" 
     android:id="@+id/textView5" 
     android:layout_alignLeft="@+id/imageView5" 
     android:layout_alignStart="@+id/imageView5" 
     android:textAlignment="center" 
     android:textStyle="normal|bold" 
     android:textColor="@android:color/background_light" /> 

    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/textView5" 
     android:layout_alignRight="@+id/imageView6" 
     android:layout_alignEnd="@+id/imageView6" 
     android:id="@+id/textView6" 
     android:layout_alignLeft="@+id/imageView6" 
     android:layout_alignStart="@+id/imageView6" 
     android:textAlignment="center" 
     android:textStyle="normal|bold" 
     android:textColor="@android:color/background_light" /> 

    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/textView2" 
     android:layout_alignRight="@+id/imageView1" 
     android:layout_alignEnd="@+id/imageView1" 
     android:id="@+id/textView1" 
     android:layout_alignLeft="@+id/imageView1" 
     android:layout_alignStart="@+id/imageView1" 
     android:textAlignment="center" 
     android:textStyle="normal|bold" 
     android:textColor="@android:color/background_light" /> 

    <Button 
     android:text="Check Your Answer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="25dp" 
     android:id="@+id/button" 
     android:textAlignment="center" 
     android:textStyle="normal|bold" 
     android:textColor="@android:color/background_light" 
     android:background="@color/colorPrimary" 
     android:layout_below="@+id/edittext" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <Button 
     android:text="Reset" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="27dp" 
     android:id="@+id/button2" 
     android:background="@color/colorPrimary" 
     android:textSize="14sp" 
     tools:textColor="@android:color/background_light" /> 

</RelativeLayout> 

웹에서 몇 가지 해결책을 시도했지만 작동하지 않습니다.

+0

'원인 : java.lang.NullPointerException' – tynn

답변

1

당신의 레이아웃은 Toolbar이없는, 그럼에도 불구하고 다음과 같은 호출 :

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 

을 하나 이러한 호출을 제거하거나 레이아웃에 Toolbar를 추가합니다.

후자가 작동하려면 예를 들어 ActionBar, Theme.AppCompat.Light.NoActionBar이없는 AppCompat 테마 중 하나를 사용해야합니다 (또는에서 파생). 당신은 당신의 사건에 대한 어떤 레이아웃 XML을 설정하기 전에 뷰 요소 (텍스트 뷰, 버튼 등)을 시작할 수 없습니다

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
</style> 
0

: 당신이 뭔가를 할 것이다 당신의 styles.xml에 따라서

R.layout.activity_main 파일입니다.

package com.example.dhara.codesprint; 

import android.content.res.AssetManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity { 


    //private WordDictionary dictionary; 
    private String currentWord; 
    private ArrayList<String> words; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     TextView text1 = (TextView) findViewById(R.id.textView1); 
     TextView text2 = (TextView) findViewById(R.id.textView2); 
     TextView text3 = (TextView) findViewById(R.id.textView3); 
     TextView text4 = (TextView) findViewById(R.id.textView4); 
     TextView text5 = (TextView) findViewById(R.id.textView5); 
     TextView text6 = (TextView) findViewById(R.id.textView6); 
     TextView text7 = (TextView) findViewById(R.id.edittext); 
     Button checkAnswer = (Button) findViewById(R.id.button); 
     Button reset = (Button) findViewById(R.id.button2); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     AssetManager assetManager = getAssets(); 
     try { 
      InputStream inputStream = assetManager.open("words.txt"); 
     } catch (IOException e) { 
      Toast toast = Toast.makeText(this, "Could not load dictionary", Toast.LENGTH_LONG); 
      toast.show(); 
     } 


     reset.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

      } 
     }); 



     checkAnswer.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

      } 
     }); 
    } 
} 
0

에서 onCreate()에서 레이아웃 요소를 초기화하십시오 때문에 활동은에서 onCreate() 메서드를 호출하지만, 여기에 당신이 당신의에서 onCreate() 메소드에서 레이아웃을 요구하고 있지만, 그 후에는 초기화되지 않습니다 시작 얻을 때 요소들, 또한 매니페스트에 활동 이름을 추가했는지 여부를 확인하십시오.주의 깊게 읽고 구현하십시오. 도움이 되길 바랍니다.

관련 문제