0

我有两个课程。我希望当用户单击两个第一个按钮时,程序转到第一类(主类)。我不使用片段。我编写了这段代码,但是当运行程序并单击 Places 按钮时,程序已停止。为什么?(在我添加按钮点击之前,程序正确运行)。LogCat 写入 nullexception 但我找不到 null 在哪里?!请帮我。

位置.java

public class Location extends MapActivity {
private FrameLayout linear;
private MapView map;
private MapController controller;
private EditText text_location;
GeoPoint searchLocation;
GeoPoint mylocation;
double myLatitude=0;
double myLongitude=0;
List<Overlay> mapOverlays;
Drawable drawable;
SimpleItemizedOverlay itemizedOverlay;

//Other classes
private Button mHome;
private Button mProduct;
private Button mPlaces;
private WebView mWebView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location);

    initOtherButtons();
            //....
    }
    private void initOtherButtons(){
    mHome=(Button)findViewById(R.id.button_home);
    mProduct=(Button)findViewById(R.id.button_product);
    mPlaces=(Button)findViewById(R.id.button_places);
    mWebView=(WebView)findViewById(R.id.web_view);
    //TEST
    mHome.setOnClickListener(onClickListener);
    mProduct.setOnClickListener(onClickListener);

}
private OnClickListener onClickListener=new OnClickListener(){
    Intent i=new Intent(Location.this,Main.class);
    public void onClick(View v){
        switch(v.getId()){
        case R.id.button_home:
            startActivity(i);
            break;
        case R.id.button_product:
            startActivity(i);
            break;
        }
    }
};
}

主.java

public class Main extends Activity {

private WebView mWebView;
private Button mHome;
private Button mProduct;
private Button mPlaces;

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

mWebView = (WebView)findViewById(R.id.web_view);
mWebView.loadUrl("http://www.google.com");

mHome = (Button)findViewById(R.id.button_home);
mProduct=(Button)findViewById(R.id.button_product);
mPlaces=(Button)findViewById(R.id.button_places);

mHome.setOnClickListener(onClickListener);
mProduct.setOnClickListener(onClickListener);
mPlaces.setOnClickListener(onClickListener);

}
private OnClickListener onClickListener=new OnClickListener(){
public void onClick(View v){
    switch(v.getId()){
    case R.id.button_home:
        mWebView.loadUrl("http://www.google.com");
        break;
    case R.id.button_product:
        mWebView.loadUrl("http://www.google.com");
        break;
    case R.id.button_places:
        Intent i=new Intent(Main.this,Location.class);
        startActivity(i);
        break;
    }
}
};
}

清单.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.loyaltier"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />



<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"

    android:theme="@style/AppTheme" >
    <uses-library android:name="com.google.android.maps" />  
    <activity
        android:name=".Main"
        android:label="@string/title_activity_main" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Location"
        android:label="@string/Location_title">
        </activity>
        <activity android:name=".SimpleItemizedOverlay"
        android:label="@string/app_name">
        </activity>

    <activity android:name=".BalloonOverlayView"
        android:label="@string/app_name">
        </activity>

    <activity android:name=".BalloonItemizedOverlay"
        android:label="@string/app_name">
        </activity>
</application>

</manifest>

日志猫:

11-11 09:56:16.506: D/OpenGLRenderer(18214): Flushing caches (mode 1)
11-11 09:56:16.506: D/OpenGLRenderer(18214): Flushing caches (mode 0)
11-11 09:56:38.006: D/dalvikvm(18275): GC_CONCURRENT freed 172K, 2% free 14351K/14599K, paused 9ms+2ms
11-11 09:56:38.053: D/dalvikvm(18275): GC_FOR_ALLOC freed 7K, 2% free 14715K/14983K, paused 11ms
11-11 09:56:39.287: D/AndroidRuntime(18275): Shutting down VM
11-11 09:56:39.287: W/dalvikvm(18275): threadid=1: thread exiting with uncaught exception (group=0x40a891f8)
11-11 09:56:39.295: E/AndroidRuntime(18275): FATAL EXCEPTION: main
11-11 09:56:39.295: E/AndroidRuntime(18275): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.example.loyaltier/org.example.loyaltier.Location}: java.lang.NullPointerException
11-11 09:56:39.295: E/AndroidRuntime(18275):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at android.os.Looper.loop(Looper.java:137)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at android.app.ActivityThread.main(ActivityThread.java:4340)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at java.lang.reflect.Method.invokeNative(Native Method)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at java.lang.reflect.Method.invoke(Method.java:511)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at dalvik.system.NativeStart.main(Native Method)
11-11 09:56:39.295: E/AndroidRuntime(18275): Caused by: java.lang.NullPointerException
11-11 09:56:39.295: E/AndroidRuntime(18275):    at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at android.content.ComponentName.<init>(ComponentName.java:75)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at android.content.Intent.<init>(In tent.java:3004)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at org.example.loyaltier.Location$1.<init>(Location.java:118)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at org.example.loyaltier.Location.<init>(Location.java:117)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at java.lang.Class.newInstanceImpl(Native Method)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at java.lang.Class.newInstance(Class.java:1319)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
11-11 09:56:39.295: E/AndroidRuntime(18275):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
11-11 09:56:39.295: E/AndroidRuntime(18275):    ... 11 more

编辑:简单程序

private OnClickListener onClickListener=new OnClickListener(){
    public void onClick(View v){
        switch(v.getId()){
        case R.id.button_home:
            mWebView.loadUrl("http://www.google.com");
            break;

        }
    }
};
4

4 回答 4

1

可能是您的 manifest.xml 中的活动声明不正确(或根本没有声明)。验证清单并确保声明了活动并且名称正确。

于 2012-11-11T06:39:31.280 回答
0

它看起来是一个 NullPointerException:

11-11 09:56:39.295: E/AndroidRuntime(18275): Caused by: java.lang.NullPointerException

您的两个 Activity 类都在清单中吗?如果是这样,请检查 Main 的 NPE 的 onCreate 或在此处发布您的代码。

于 2012-11-11T06:40:13.243 回答
0

如果您在模拟器中运行它,请检查您的模拟器是否已创建支持 MapView 所需的 Google API。查看此链接以了解AVD设置。

EDIT01:请尝试Intent i=new Intent(Location.this,Main.class);按照onClick()以下方法移动:

private OnClickListener onClickListener=new OnClickListener(){

    public void onClick(View v){
    Intent i=new Intent(Location.this,Main.class);
        switch(v.getId()){
        case R.id.button_home:
            startActivity(i);
            break;
        case R.id.button_product:
            startActivity(i);
            break;
        }
    }
};
于 2012-11-11T06:57:22.843 回答
0

我认为问题出在 Intent 对象上,因为我更改了程序,程序正确运行。发生了什么?为什么 Intent 应该在每种情况下都反对?

public void onClick(View v){
switch(v.getId()){
case R.id.button_home:
        Intent i=new Intent(Location.this,Main.class);
    startActivity(i);
    break;
case R.id.button_product:
    Intent j=new Intent(Location.this,Main.class);
    startActivity(j);
    break;
case R.id.button_places:
    //
    break;
}
}

谢谢。干杯

于 2012-11-11T09:14:42.707 回答