-6

嗨,函数 startActivity 引发了一个空指针异常,这是我的类:

public class DetailActivity extends Activity {

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

    }

    public void showHtml(String prod){

        Intent myIntent = new Intent(getBaseContext(), DetailActivity.class);
        startActivity(myIntent);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.activity_main, menu);
        return false;
    }
}

这是我的清单

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.xxxx"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.INTERNET" />

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="16" />

    <application   android:theme="@android:style/Theme.NoTitleBar" android:allowBackup="false">
        <activity
            android:name="com.example.xxxx.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="com.example.xxxx.fragments.frag1.BarcodeInsertActivity" />
        <activity android:name="com.example.xxxx.fragments.frag2.ShopBasketActivity" />
        <activity android:name="com.example.xxxx.fragments.frag3.CheckoutActivity" />
        <activity android:name="com.example.xxxx.fragments.frag4.DetailActivity" />  
        <activity
            android:name="com.google.zxing.client.android.CaptureActivity"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:label="ZXing"
            android:windowSoftInputMode="stateAlwaysHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.zxing.client.android.SCAN" />

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



    </application>

</manifest>

这就是我尝试执行该功能的方式

 DetailActivity gg= new DetailActivity();

gg.showHtml( "some html code <div class='someclass'>asdas</div> and other html tags");

如果有人可以帮助我尝试很多没有用的东西会很好:)

4

2 回答 2

4

DetailActivity 就是这样,一个 Activity。您不能像访问对象一样访问其中的方法。这样做的唯一方法是将该方法设为静态并执行 D etailActivity.showHtml(),但建议这样做。

另一个问题是您没有正确创建该活动。您需要像这样启动它:

Intent newIntent = new Intent(this, DetailActivity.class);
startActivity(newIntent);

然后,您应该在该活动中访问该方法,或者像这样在包中传递变量:

Bundle bundle = new Bundle();
bundle.putString("webAdd", string);
newIntent.putExtras(bundle);

然后,您可以通过执行以下操作在 DetailActivity 中访问它:

Bundle bundle = this.getIntent().getExtras();
String newString = bundle.getString("webAdd");

我建议阅读一些关于 Activies 如何工作的教程。

于 2013-03-07T09:22:26.513 回答
0

In android you don't need to write

DetailActivity gg= new DetailActivity();

You are already in they write activity and you don't need this any more.
You also need to put this activity into your manifest xml file

DetailActivity
于 2013-03-07T09:27:18.003 回答