0

「OK」ボタンをクリックすると、強制終了します。startActivity() 関数に問題があります。[OK] ボタンをクリックすると、アプリが強制終了し、LogCat が以下のエラーをスローします。MainActivity.java の startactivity() メソッドに問題があります

MainActivity.java

package com.location.sensor;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
            Button btn=(Button) findViewById(R.id.btn_OK);
            Animation anim= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.jump);
            btn.startAnimation(anim);
            btn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Animation anim= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.jump);
                    anim.setDuration(3000);
                    v.startAnimation(anim);
                    TextView n=new TextView(getBaseContext());
                    EditText txt=(EditText) findViewById(R.id.txt_username);
                    //Animation anim= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.jump);
                    //n.startAnimation(anim);
                    String s=txt.getText().toString();
                    n.setText(s);
                    n.setBackgroundResource(R.drawable.icn);
                    //Intent i=new Intent("com.location.pendintent");
                    //Bundle extras=new Bundle();
                    //extras.putString("alarm", s);
                    //i.putExtras(extras);
                    try {
                        startActivity(new Intent("com.hello.loc"));
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        Log.d("service", "Caught here!");
                    }

                }
            });

    }
}

NotificationClass.java

package com.location.sensor;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class NotificationClass extends Activity {
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        //String data=getIntent().getExtras().getString("alarm").toString();
        TextView tv=(TextView) findViewById(R.id.txt_pendint);
        Button btn=(Button) findViewById(R.id.btn_cancel);
        //tv.setText(data);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(0);

            }
        });
        setContentView(R.layout.notif);
    }
protected Dialog onCreateDialog(int id)
{
    switch(id)
    {
    case 0:

        return new AlertDialog.Builder(this)
                .setIcon(R.drawable.excl_mark)
                .setTitle("Do you want to Cancel?")
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        finish();
                    }
                })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getBaseContext(), "Cancel Clicked!", Toast.LENGTH_LONG).show();
                    }
                })
                .create();
    }
    return null;

}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.location.sensor"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10"/>

    <application
        android:icon="@drawable/gps1"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            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=".NotificationClass"
            android:label="YOU ARE IN PROXIMITY AREA!!!">
            <intent-filter>
               <action android:name="com.hello.loc"/> 
               <category  android:name="android.intent.category.DEFAULT" /> 
             </intent-filter>   
        </activity>
    </application>

</manifest>

LogCat

08-01 00:42:20.665: E/Trace(808): error opening trace file: No such file or directory (2)
08-01 00:42:51.165: E/AndroidRuntime(808): FATAL EXCEPTION: main
08-01 00:42:51.165: E/AndroidRuntime(808): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.location.sensor/com.location.sensor.NotificationClass}: java.lang.NullPointerException
08-01 00:42:51.165: E/AndroidRuntime(808):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-01 00:42:51.165: E/AndroidRuntime(808):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-01 00:42:51.165: E/AndroidRuntime(808):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-01 00:42:51.165: E/AndroidRuntime(808):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-01 00:42:51.165: E/AndroidRuntime(808):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 00:42:51.165: E/AndroidRuntime(808):  at android.os.Looper.loop(Looper.java:137)
08-01 00:42:51.165: E/AndroidRuntime(808):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-01 00:42:51.165: E/AndroidRuntime(808):  at java.lang.reflect.Method.invokeNative(Native Method)
08-01 00:42:51.165: E/AndroidRuntime(808):  at java.lang.reflect.Method.invoke(Method.java:511)
08-01 00:42:51.165: E/AndroidRuntime(808):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-01 00:42:51.165: E/AndroidRuntime(808):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 00:42:51.165: E/AndroidRuntime(808):  at dalvik.system.NativeStart.main(Native Method)
08-01 00:42:51.165: E/AndroidRuntime(808): Caused by: java.lang.NullPointerException
08-01 00:42:51.165: E/AndroidRuntime(808):  at com.location.sensor.NotificationClass.onCreate(NotificationClass.java:23)
08-01 00:42:51.165: E/AndroidRuntime(808):  at android.app.Activity.performCreate(Activity.java:5008)
08-01 00:42:51.165: E/AndroidRuntime(808):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-01 00:42:51.165: E/AndroidRuntime(808):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-01 00:42:51.165: E/AndroidRuntime(808):  ... 11 more

通知.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
 <Button
     android:text="Cancel"
     android:id="@+id/btn_cancel"
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:background="#FF4000"
     />
 <LinearLayout 
     android:padding="5px"
     android:orientation="vertical">
     <TextView 
         android:id="@+id/txt_pendint"
         android:textSize="18px"
         android:text="No text Added"
         android:textColor="#A9F5E1"
         android:background="#0B3B39"/>
 </LinearLayout>

</RelativeLayout>
4

2 に答える 2

3

findViewById()コンテンツビューを設定せずにNotificationClassアクティビティを呼び出しています。リソースが見つからないため、Buttonnullです。を呼び出しsetContentView(R.layout.yourlayout)ます。

于 2012-07-31T19:44:57.673 に答える
2

私はあなたがsetContentView(R.layout.notif);あなたの下に置く必要があると思いますsuper.onCreate(savedInstanceState);

public class NotificationClass extends Activity {
    public void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);
        setContentView(R.layout.notif);
        //Rest of code.
    }

編集:「エクスポートされたアクティビティには許可は必要ありません」の場合android:exported="false"は、次のようにアクティビティプロパティに追加してみてください。

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

お役に立てば幸いです。

于 2012-07-31T19:45:49.137 に答える