** 晚上好,
我的应用程序中的意图有问题,当我点击按钮“添加”它们不起作用并向我显示“添加活动”,** 这是mainActivity
package com.hema.colornotes;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.btAdd);
b2 = (Button) findViewById(R.id.btShow);
}
public void onAdd(){
Intent i =new Intent(MainActivity.this,add.class);
startActivity(i);
}
public void onShow(){
Intent i =new Intent(MainActivity.this,show.class);
startActivity(i);
}
}
这就是添加类
package com.hema.colornotes;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
public class add extends ActionBarActivity {
EditText note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
}
public void onSave() {
note = (EditText) findViewById(R.id.etNote);
String notes = note.getText().toString();
Intent i = new Intent(this, show.class);
i.putExtra("message", notes);
startActivity(i);
}
public void onCancel()
{
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}
那是show class
package com.hema.colornotes;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class show extends ActionBarActivity {
Context context;
List<String> tasks;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
context = this;
tasks = new ArrayList<String>();
// second parameter is row layout,
adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1,tasks);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
Intent intent = getIntent();
String value = intent.getExtras().getString("message");
tasks.add(value);
// this method will refresh your listview manually
adapter.notifyDataSetChanged();
}
}
提前谢谢你 我希望有人回答我的问题。
答案 0 :(得分:0)
Have to stated the new intent activity in your Manifest file.
You need to post log cat as well to make it easier to solve for us...
<activity
android:name=".add"
android:label="@string/app_name"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>`
<activity
android:name=".show"
android:label="@string/app_name"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
EDIT: I just realized what you wanted you have to call onClickListener on the buttons so:
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onAdd();
}
});`
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onShow();
}
});`