1. ホーム
  2. android

[解決済み] Androidアプリがアクティビティを開始できない コンポーネントinfo

2022-02-02 22:14:59

質問内容

私は新人Androidプログラマーですが、最近、私のプロジェクトの多くでこのエラーが発生するようになりました。

07-31 23:45:19.592: ERROR/AndroidRuntime(716): java.lang.RuntimeException: Unable to start     

activity ComponentInfo{com.amrit.musifind/com.amrit.musifind.Main}: 

java.lang.NullPointerException

どなたかこの問題を解決する手助けをしていただけませんか?

logcatのスタックトレース全体はこちらです。

07-31 23:45:18.512: INFO/ActivityManager(63): Starting activity: Intent {     
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000   
cmp=com.amrit.musifind/.Main }
07-31 23:45:18.682: INFO/ActivityManager(63): Start proc com.amrit.musifind for activity   
com.amrit.musifind/.Main: pid=716 uid=10025 gids={3003}
07-31 23:45:18.962: INFO/jdwp(716): received file descriptor 20 from ADB
07-31 23:45:19.102: DEBUG/ddm-heap(716): Got feature list request
07-31 23:45:19.572: DEBUG/AndroidRuntime(716): Shutting down VM
07-31 23:45:19.572: WARN/dalvikvm(716): threadid=3: thread exiting with uncaught   
exception (group=0x4001aa28)
07-31 23:45:19.572: ERROR/AndroidRuntime(716): Uncaught handler: thread main exiting due 
to uncaught exception
07-31 23:45:19.592: ERROR/AndroidRuntime(716): java.lang.RuntimeException: Unable to    
start activity ComponentInfo{com.amrit.musifind/com.amrit.musifind.Main}:    
java.lang.NullPointerException
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at    
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at    
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at    
android.app.ActivityThread.access$2100(ActivityThread.java:116)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
android.os.Handler.dispatchMessage(Handler.java:99)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at       
android.os.Looper.loop(Looper.java:123)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
android.app.ActivityThread.main(ActivityThread.java:4203)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at    
java.lang.reflect.Method.invokeNative(Native Method)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
java.lang.reflect.Method.invoke(Method.java:521)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
dalvik.system.NativeStart.main(Native Method)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): Caused by: java.lang.NullPointerException
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
com.amrit.musifind.Main.onCreate(Main.java:44)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at     
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     ... 11 more
07-31 23:45:19.612: INFO/Process(63): Sending signal. PID: 716 SIG: 3
07-31 23:45:19.612: INFO/dalvikvm(716): threadid=7: reacting to signal 3
07-31 23:45:19.642: INFO/dalvikvm(716): Wrote stack trace to '/data/anr/traces.txt'
07-31 23:45:21.422: INFO/Process(716): Sending signal. PID: 716 SIG: 9
07-31 23:45:21.442: INFO/ActivityManager(63): Process com.amrit.musifind (pid 716) has   
died.
07-31 23:45:21.583: WARN/UsageStats(63): Unexpected resume of com.android.launcher while    
already resumed in com.amrit.musifind
07-31 23:45:21.712: WARN/InputManagerService(63): Window already focused, ignoring focus    
gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@439559d0
07-31 23:45:23.482: DEBUG/AndroidRuntime(725): >>>>>>>>>>>>>> AndroidRuntime START    
<<<<<<<<<<<<<<
07-31 23:45:23.482: DEBUG/AndroidRuntime(725): CheckJNI is ON
07-31 23:45:23.812: DEBUG/AndroidRuntime(725): --- registering native functions ---
07-31 23:45:23.832: INFO/jdwp(725): received file descriptor 20 from ADB
07-31 23:45:24.193: DEBUG/ddm-heap(725): Got feature list request
07-31 23:45:25.052: DEBUG/AndroidRuntime(725): Shutting down VM
07-31 23:45:25.052: DEBUG/dalvikvm(725): DestroyJavaVM waiting for non-daemon threads to   
exit
07-31 23:45:25.062: DEBUG/dalvikvm(725): DestroyJavaVM shutting VM down
07-31 23:45:25.062: DEBUG/dalvikvm(725): HeapWorker thread shutting down
07-31 23:45:25.062: DEBUG/dalvikvm(725): HeapWorker thread has shut down
07-31 23:45:25.073: DEBUG/jdwp(725): JDWP shutting down net...
07-31 23:45:25.073: DEBUG/jdwp(725): +++ peer disconnected
07-31 23:45:25.073: INFO/dalvikvm(725): Debugger has detached; object registry had 1   
entries
07-31 23:45:25.093: DEBUG/dalvikvm(725): VM cleaning up
07-31 23:45:25.113: DEBUG/dalvikvm(725): LinearAlloc 0x0 used 665652 of 4194304 (15%)
07-31 23:47:22.712: DEBUG/dalvikvm(104): GC freed 9173 objects / 524184 bytes in 149ms

1つ目のアクティビティはこちら:start.java

package com.amrit.musifind;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.amrit.musifind.R;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public abstract class start extends Activity implements OnClickListener {

Button buttonGo, buttonReset;
EditText EditTextinput;
String input = "";
String ur = "http://www.tastekid.com/ask/ws?q=";
String l = "&f=musifin2125&k=mjjlnzkyzwuz&format=JSON";
String url = "" ;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    buttonGo = (Button) findViewById(R.id.buttonGo);
    buttonReset = (Button) findViewById(R.id.buttonReset);

    EditTextinput = (EditText) findViewById(R.id.EditTextinput);

    //Button listener
    buttonReset.setOnClickListener(this);
    buttonGo.setOnClickListener(this);
}

public void onClick (View src){
    switch(src.getId()){
        case R.id.buttonGo:
            input = EditTextinput.getText().toString();
            url = ur + input + l  ;

            Intent Main = new Intent(this, ToClass.class);
            Main.putExtra("userurl", url);
            startActivity(Main);
            break;

        case R.id.buttonReset:
            EditTextinput.setText("");
            break;
        }
    }
}

2つ目のアクティビティです。Main.java

package com.amrit.musifind;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.amrit.musifind.JSONfunctions;
import com.amrit.musifind.R;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listplaceholder);

    Intent intent = getIntent();
    String url = intent.getExtras().getString("userurl");

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>    
    ();

    JSONObject json = JSONfunctions.getJSONfromURL(url);

    try{
        JSONObject earthquakes = json.getJSONObject("Similar");
        JSONArray info = earthquakes.getJSONArray("Results");

        for (int i = 0; i < info.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject e = info.getJSONObject(i);

            map.put("id", String.valueOf(i));
            map.put("name", "Name:" + e.getString("Name"));
            map.put("type", "Type: " + e.getString("Type"));
            mylist.add(map);
        }

        JSONArray  results = json.getJSONArray("Results");

        for(int i=0;i<results.length();i++){                        
            HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject e = results.getJSONObject(i);

            map.put("id",  String.valueOf(i));
            map.put("name", "Name:" + e.getString("name"));
            map.put("type", "Type: " +  e.getString("type"));
            mylist.add(map);            
        }       
    } catch(JSONException e) {
         Log.e("log_tag", "Error parsing data "+e.toString());
    }

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
         new String[] { "name", "type" }, 
         new int[] { R.id.item_title, R.id.item_subtitle });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);  
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
            Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", 
                Toast.LENGTH_SHORT).show(); 
            }
        });
    }
}

最後に、私のマニフェストファイルです。AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.amrit.musifind"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".start" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.start" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name= ".Main">
            <intent-filter>
                <action android:name="android.intent.action.Main" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />

</manifest>

この問題を解決しないと、私のアプリをテストすることができないのです。

解決方法は?

ヌルポインタの例外はこの行にあるようです。

String url = intent.getExtras().getString("userurl");

なぜなら intent.getExtras() は、インテントがエクストラを持たない場合、null を返します。

このコードの断片を認識する必要があります。

Intent Main = new Intent(this, ToClass.class);
Main.putExtra("userurl", url);
startActivity(Main);

はMain.javaで書いたアクティビティを開始せず、次のアクティビティを開始しようとします。 ToClass が存在しない場合、アプリはクラッシュします。

また "android.intent.action.start" ということで、マニフェストはもっとこうあるべきでしょう。

<activity android:name=".start" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name= ".Main">
</activity>

しかし、アンドロイド開発のためのチュートリアルをチェックし、そこから発展させることを強くお勧めします。