-2

现在,我正在对我的 android 模拟器进行强制关闭。

完成此应用程序后,我将要放入一个自定义字段而不是仅进行测试,但现在我只想从 http 活动中显示测试。

任何帮助都会很棒!

主要活动:

public class MainActivity extends Activity {

public final static String EXTRA_MESSAGE = "com.example.main.MESSAGE";


/*@SuppressLint("ParserError")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}*/

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

private Button searchBtn;

@Override
protected void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);
    setContentView(R.layout.activity_main);

    searchBtn = (Button) findViewById(R.id.button1);

    searchBtn.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent intent = new Intent(null, http.class);
            startActivity(intent);
        }
    });

}   
}

网址:

public class http extends Activity {

public http(){
    httpMethod();
}


public void httpMethod(){
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://api.site.com/api/");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
       ;

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        String test = "hello";

        TextView myTextView = (TextView) findViewById(R.id.myTextView);
        myTextView.setText(test);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}    
 }

显现:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        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="com.example.main.DisplayMessageActivity"/>
    <activity android:name="com.example.main.http"/>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
4

4 回答 4

3

代码中有很多问题:

1) Intent intent = new Intent(null, http.class);

使用第一个参数作为 MainActivity.class 而不是 null

2)httpActivity应该有onCreate(或)onResume生命周期活动方法来创建活动startActivity

不是但最不重要的是,请花一些时间阅读文档和做示例程序,而不是仅仅输入一些东西并在 SO 上发布。通过解决你所有的问题,就像 SO 社区为你做了你的应用一样。

于 2012-07-09T19:52:00.787 回答
1

开始为:

searchBtn.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent intent = new Intent(MainActivity.this, http.class);
            startActivity(intent);
        }
    });

null而不是在意图构造函数中作为第一个参数传递

有关更多信息,请参见此处

http://developer.android.com/reference/android/content/Intent.html

于 2012-07-09T19:54:00.207 回答
1

你必须Intent像这样初始化

Intent intent = new Intent(MainActivity.this, http.class);

您需要Context作为第一个参数而不是null传递。

于 2012-07-09T19:51:32.303 回答
0

I trully advice you to read some Android basics beacause you have some issues in the code:

  1. You have a null context when you're initializing the intent at the button's listener. You should have: Intent intent = new Intent(getApplicationContext(), http.class); or Intent intent = new Intent(MainActivity.this, http.class);

  2. You need to create your ativity and set it's content. You must override at least the onCreate method.

  3. It's not so important, but its a good practice to write code that anyone might understand instead of write code for the machine! I'm telling this because you have *activity_main* sml file where you define your main activity layout and menu. I suggest you to refractor these file names to something like main.xml, for the layout, and *main_mnu.xml*.

于 2012-07-09T20:41:49.440 回答