RunTime.Exception无法实例化活动ComponentInfo

时间:2014-06-18 22:24:52

标签: android eclipse android-intent

大家好我是android的新手,请帮帮我。我想开始" OyunActivity"来自" LevelActivity"在我的项目中。但是出现了以下错误:

06-19 01:02:43.670: E/AndroidRuntime(26550): FATAL EXCEPTION: main
06-19 01:02:43.670: E/AndroidRuntime(26550): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hangdeneme/com.example.hangdeneme.OyunActivity}: java.lang.NullPointerException
06-19 01:02:43.670: E/AndroidRuntime(26550):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at android.os.Looper.loop(Looper.java:137)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at android.app.ActivityThread.main(ActivityThread.java:4898)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at java.lang.reflect.Method.invokeNative(Native Method)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at java.lang.reflect.Method.invoke(Method.java:511)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at dalvik.system.NativeStart.main(Native Method)
06-19 01:02:43.670: E/AndroidRuntime(26550): Caused by: java.lang.NullPointerException
06-19 01:02:43.670: E/AndroidRuntime(26550):    at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:160)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at com.example.hangdeneme.OyunActivity.<init>(OyunActivity.java:34)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at java.lang.Class.newInstanceImpl(Native Method)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at java.lang.Class.newInstance(Class.java:1319)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
06-19 01:02:43.670: E/AndroidRuntime(26550):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
06-19 01:02:43.670: E/AndroidRuntime(26550):    ... 11 more

我将所有活动添加到AndroidManifest.xml中就像这样: Android Manifest.xml

<activity
        android:name="com.example.hangdeneme.LevelActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.LevelActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.hangdeneme.OyunActivity"
        android:label="@string/title_activity_oyun" >
        <intent-filter>
            <action android:name="android.intent.action.OyunActivity" />

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

我尝试通过在java构建路径上添加库来解决问题,但它不起作用。 有人认为这是由日食引起的问题,但我不知道。 无论如何,在项目我的LevelActivity.java是那个,如果有问题,我认为它在这里:

package com.example.hangdeneme;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class LevelActivity extends Activity {

public static String KATEGORI_NU="";
int kategoriNu=0;

ListView lisViewKategori;


String kategori[]={"bitki","hayvan","özel","şehir","film","müzik","marka","falan-filan"};
MainMenuActivity mma= new MainMenuActivity();



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

    ArrayAdapter<String> adapterKategori = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,kategori);

    lisViewKategori = (ListView) findViewById(R.id.listView1);
    lisViewKategori.setAdapter(adapterKategori);
    lisViewKategori.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            kategoriNu=arg2;
            Intent intent= new Intent(LevelActivity.this,OyunActivity.class);
            intent.putExtra(KATEGORI_NU, kategoriNu);
            mma.menuMedia.pause();
            startActivity(intent);
            finish();

        }
    });

}
}

和OyunActivity.java就是这样 - 我不认为这里可能有问题 - :

package com.example.hangdeneme;

import java.util.ArrayList;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class OyunActivity extends Activity implements OnClickListener{

Button buttonA,buttonB,buttonC,buttonC2,buttonD,buttonE,buttonF,buttonG,buttonG2,buttonH,buttonI,buttonI2,buttonJ,buttonK,buttonL;
Button buttonM,buttonN,buttonO,buttonO2,buttonP,buttonR,buttonS,buttonS2,buttonT,buttonU,buttonU2,buttonV,buttonY,buttonZ;
static ImageView hangmanImage;
TextView textViewHata;
TextView textViewSonuc;
MainMenuActivity mma= new MainMenuActivity();
MediaPlayer butonses;
private Vibrator vib;
SharedPreferences preference = getSharedPreferences("TERCİHLER", MODE_PRIVATE);
private static final String PREF_TITRESIM="PREF_TITRESIM";
private static final String PREF_SES="PREF_SES";

KelimeKutuphane kk= new KelimeKutuphane();
char[] mHarfler={};
static int hataSayisi=0,oyunKontrol=0,levelNu=1,kategoriNu=0,levelDurum=0;
static final int KAZANDINIZ=0,KAYBETTİNİZ=1;    
String kelime="";
ArrayList<TextView> harf = new ArrayList<TextView>();
Intent intent = new Intent();   

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

    vib=(Vibrator) getSystemService(VIBRATOR_SERVICE);
    buttonA = (Button) findViewById(R.id.buttonA);
    buttonB = (Button) findViewById(R.id.buttonB);
    buttonC = (Button) findViewById(R.id.buttonC);
    buttonC2 = (Button) findViewById(R.id.buttonC2);
    buttonD = (Button) findViewById(R.id.buttonD);
    buttonE = (Button) findViewById(R.id.buttonE);
    buttonF = (Button) findViewById(R.id.buttonF);
    buttonG = (Button) findViewById(R.id.buttonG);
    buttonG2 = (Button) findViewById(R.id.buttonG2);
    buttonH = (Button) findViewById(R.id.buttonH);
    buttonI = (Button) findViewById(R.id.buttonI);
    buttonI2 = (Button) findViewById(R.id.buttonI2);
    buttonJ = (Button) findViewById(R.id.buttonJ);
    buttonK = (Button) findViewById(R.id.buttonK);
    buttonL = (Button) findViewById(R.id.buttonL);
    buttonM = (Button) findViewById(R.id.buttonM);
    buttonN = (Button) findViewById(R.id.buttonN);
    buttonO = (Button) findViewById(R.id.buttonO);
    buttonO2 = (Button) findViewById(R.id.buttonO2);
    buttonP = (Button) findViewById(R.id.buttonP);
    buttonR = (Button) findViewById(R.id.buttonR);
    buttonS = (Button) findViewById(R.id.buttonS);
    buttonS2 = (Button) findViewById(R.id.buttonS2);
    buttonT = (Button) findViewById(R.id.buttonT);
    buttonU = (Button) findViewById(R.id.buttonU);
    buttonU2 = (Button) findViewById(R.id.buttonU2);
    buttonV = (Button) findViewById(R.id.buttonV);
    buttonY = (Button) findViewById(R.id.buttonY);
    buttonZ = (Button) findViewById(R.id.buttonZ);

    buttonA.setOnClickListener(this);
    buttonB.setOnClickListener(this);
    buttonC.setOnClickListener(this);
    buttonC2.setOnClickListener(this);
    buttonD.setOnClickListener(this);
    buttonE.setOnClickListener(this);
    buttonF.setOnClickListener(this);
    buttonG.setOnClickListener(this);
    buttonG2.setOnClickListener(this);
    buttonH.setOnClickListener(this);
    buttonI.setOnClickListener(this);
    buttonI2.setOnClickListener(this);
    buttonJ.setOnClickListener(this);
    buttonK.setOnClickListener(this);
    buttonL.setOnClickListener(this);
    buttonM.setOnClickListener(this);
    buttonN.setOnClickListener(this);
    buttonO.setOnClickListener(this);
    buttonO2.setOnClickListener(this);
    buttonP.setOnClickListener(this);
    buttonR.setOnClickListener(this);
    buttonS.setOnClickListener(this);
    buttonS2.setOnClickListener(this);
    buttonT.setOnClickListener(this);
    buttonU.setOnClickListener(this);
    buttonU2.setOnClickListener(this);
    buttonV.setOnClickListener(this);
    buttonY.setOnClickListener(this);
    buttonZ.setOnClickListener(this);

    textViewHata = (TextView) findViewById(R.id.textViewHata);
    textViewSonuc= (TextView) findViewById(R.id.textViewSonuc);

    hangmanImage = (ImageView) findViewById(R.id.hangmanImage);

    new MediaPlayer();
    butonses = MediaPlayer.create(OyunActivity.this, R.raw.button);

    kelime= getKelime();

    Adapter(kelime);

}

public String getKelime(){

    Intent intent = getIntent();
    kategoriNu=intent.getIntExtra(LevelActivity.KATEGORI_NU, 2);

    int random;
    Random r= new Random();
    random=r.nextInt(2);

    switch (levelNu) {
    case 1:
        kelime= kk.dizi1[kategoriNu][random];
        break;
    case 2:
        kelime= kk.dizi2[kategoriNu][random];
        break;
    case 3:
        kelime= kk.dizi3[kategoriNu][random];
        break;
    case 4:
        kelime= kk.dizi4[kategoriNu][random];
        break;
    case 5:
        kelime= kk.dizi5[kategoriNu][random];
        break;
    case 6:
        kelime= kk.dizi6[kategoriNu][random];
        break;
    case 7:
        kelime= kk.dizi7[kategoriNu][random];
        break;
    case 8:
        kelime= kk.dizi8[kategoriNu][random];
        break;
    case 9:
        kelime= kk.dizi9[kategoriNu][random];
        break;
    case 10:
        kelime= kk.dizi10[kategoriNu][random];
        break;
    default:
        break;      
    }
    return kelime;
}

public void Adapter(String kelime){

    String mKelime = kelime;

    LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
    LinearLayout layout2 = (LinearLayout) findViewById(R.id.linearLayout2);

    mHarfler = mKelime.toCharArray();

    LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View wordView[] = new View[mHarfler.length];

    for(int i = 0 ; i<mHarfler.length ; i++){
        wordView[i] = mInflater.inflate(R.layout.a_letter_of_word, null); 
        TextView tv = (TextView) wordView[i].findViewById(R.id.letterText);
        harf.add(tv);
        harf.get(i).setText(mHarfler, i, 1);

        harf.get(i).setVisibility(android.view.View.INVISIBLE);

        if(i<9)
            layout.addView(wordView[i]);
        else
            layout2.addView(wordView[i]);
    }}

@SuppressWarnings("deprecation")
public void SearchFor(char letter){
    int i,j=0;
    if(hataSayisi<5&&oyunKontrol<mHarfler.length){
    for(i=0;i<=(mHarfler.length-1);i++){
        if(mHarfler[i]==letter){
            harf.get(i).setVisibility(android.view.View.VISIBLE);
            j=j+1;
            oyunKontrol=oyunKontrol+1;
        }           
        }
    if(j==0){
        hataSayisi=hataSayisi+1;
    textViewHata.setText("5 hata hakkınız vardır: Hata sayısı : "+hataSayisi);
    setHangImage(hataSayisi);

    }}
    else{
        if(oyunKontrol==mHarfler.length)
        {textViewSonuc.setText("OYUN BİTTİ : KAZANDINIZ");
        showDialog(KAZANDINIZ);
        oyunKontrol=0;
        hataSayisi=0;
        if(levelDurum<5)
        levelDurum=levelDurum+1;
        else{
            levelDurum=1;
            levelNu=levelNu+1;
        }
        }
        if(hataSayisi==5)
            {textViewSonuc.setText("OYUN BİTTİ : KAYBETTİNİZ");
        showDialog(KAYBETTİNİZ);
        hataSayisi=0;
        oyunKontrol=0;}
    }}

@Override
public void onClick(View v) {
    boolean btnses = preference.getBoolean(PREF_SES, true);
    boolean titresim= preference.getBoolean(PREF_TITRESIM, true);
    try{
        if(kelime!="")
        switch (v.getId()) {
    case R.id.buttonA:
        buttonA.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
            butonses.start();
        SearchFor('a');
        break;
    case R.id.buttonB:
        buttonB.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('b');
        break;
    case R.id.buttonC:
        buttonC.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('c');
        break;
    case R.id.buttonC2:
        buttonC2.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('ç');
        break;
    case R.id.buttonD:
        buttonD.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('d');
        break;
    case R.id.buttonE:
        buttonE.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('e');
        break;
    case R.id.buttonF:
        buttonF.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('f');
        break;
    case R.id.buttonG:
        buttonG.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('g');
        break;
    case R.id.buttonG2:
        buttonG2.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('ğ');
        break;
    case R.id.buttonH:
        buttonH.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('h');
        break;
    case R.id.buttonI:
        buttonI.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('ı');
        break;
    case R.id.buttonI2:
        buttonI2.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('i');
        break;
    case R.id.buttonJ:
        buttonJ.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('j');
        break;
    case R.id.buttonK:
        buttonK.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('k');
        break;
    case R.id.buttonL:
        buttonL.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('l');
        break;
    case R.id.buttonM:
        buttonM.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('m');
        break;
    case R.id.buttonN:
        buttonN.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('n');
        break;
    case R.id.buttonO:
        buttonO.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('o');
        break;
    case R.id.buttonO2:
        buttonO2.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('ö');
        break;
    case R.id.buttonP:
        buttonP.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('p');
        break;
    case R.id.buttonR:
        buttonR.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('r');
        break;
    case R.id.buttonS:
        buttonS.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('s');
        break;
    case R.id.buttonS2:
        buttonS2.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('ş');
        break;
    case R.id.buttonT:
        buttonT.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('t');
        break;
    case R.id.buttonU:
        buttonU.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('u');
        break;
    case R.id.buttonU2:
        buttonU2.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('ü');
        break;
    case R.id.buttonV:
        buttonV.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('v');
        break;
    case R.id.buttonY:
        buttonY.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('y');
        break;
    case R.id.buttonZ:
        buttonZ.setVisibility(android.view.View.INVISIBLE);
        if(titresim)
            vib.vibrate(400);
        if(btnses)
        butonses.start();
        SearchFor('z');
        break;
    default:
        break;
    }
        else 
            Toast.makeText(getApplicationContext(), "soru kelime henüz hazır değil", Toast.LENGTH_SHORT).show();
            }
    catch(Exception e){

    }}

public static void setHangImage(int hataSayisi){
    switch (hataSayisi) {
    case 1:
        hangmanImage.setImageResource(R.drawable.hangimage_bir);
        break;
    case 2:
        hangmanImage.setImageResource(R.drawable.hangimage_iki);
        break;
    case 3:
        hangmanImage.setImageResource(R.drawable.hangimage_uc);
        break;
    case 4:
        hangmanImage.setImageResource(R.drawable.hangimage_dort);
        break;
    case 5:
        hangmanImage.setImageResource(R.drawable.hangimage_bes);
        break;

    default:
        break;
    }

}

@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch (id) {
    case KAZANDINIZ:
        dialog= getDialog(KAZANDINIZ);
        break;
    case KAYBETTİNİZ:
        dialog= getDialog(KAYBETTİNİZ);
        break;

    default:
        dialog=null;
        break;
    }
    return dialog;
}

private Dialog getDialog(int id){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("HangMan");
    switch (id) {
    case KAZANDINIZ:
        builder.setMessage("KAZANDINIZ");
        builder.setPositiveButton("Devam Et",new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                getKelime();                    
            }
        });
        builder.setNegativeButton("Ana Menü",new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(OyunActivity.this,MainMenuActivity.class);
                mma.menuMedia.start();
                startActivity(intent);  
            }
        });
        break;
    case KAYBETTİNİZ:
        builder.setMessage("KAYBETTİNİZ");
        builder.setPositiveButton("Yeni Oyun",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                intent.setAction("android.intent.action.LevelActivity");
                startActivity(intent);                  
            }
        });
        builder.setNegativeButton("Ana Menü",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(OyunActivity.this,MainMenuActivity.class);
                mma.menuMedia.start();
                startActivity(intent);                      
            }
        });
        break;
    }
    builder.setCancelable(false);
    return builder.create();
}
}

1 个答案:

答案 0 :(得分:0)

您无法在Activity ...

的主体中执行此操作
SharedPreferences preference = getSharedPreferences("TERCİHLER", MODE_PRIVATE);

将其声明为

SharedPreferences preference;

...然后在onCreate(...)获取首选项...

preference = getSharedPreferences("TERCİHLER", MODE_PRIVATE);

另一点是永远不要这样做......

MainMenuActivity mma= new MainMenuActivity();

...您无法使用Activity创建new的实例。它根本行不通。不要尝试它,也不要在public中创建Activity方法或变量,并希望它们有效。