我想从另一项活动中更改textview但不能正常工作
MainActivity(使用textview)
public static TextView textViewObj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
textViewObj = (TextView) findViewById(id.textView10);
这是onClick in另一项活动:
public void onClick8 (View view) {
//HERE IS ONCLICK MEHOD WHEN I CLICK TEXT CHANGE TO ANOTHER TEXT
MainActivity.textViewObj.setText("Text change");
//END
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivity(myIntent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.dobre);
mediaPlayer.start();
}
LOGCAT更新 当我运行android studio写这个: 当我运行android studio写这个: 当我运行android studio时写下这个:
03-16 16:45:43.750 1703-1703/com.example.tom.kvz D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
03-16 16:45:43.880 1703-1710/com.example.tom.kvz E/jdwp﹕ Failed sending reply to debugger: Broken pipe
03-16 16:45:43.880 1703-1710/com.example.tom.kvz D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
03-16 16:45:43.890 1703-1703/com.example.tom.kvz D/dalvikvm﹕ GC_FOR_ALLOC freed 65K, 5% free 3417K/3560K, paused 0ms, total 2ms
03-16 16:45:43.940 1703-1706/com.example.tom.kvz D/dalvikvm﹕ GC_CONCURRENT freed <1K, 4% free 3823K/3968K, paused 49ms+1ms, total 51ms
03-16 16:45:43.980 1703-1703/com.example.tom.kvz D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 4% free 4229K/4372K, paused 35ms, total 35ms
03-16 16:45:44.000 1703-1706/com.example.tom.kvz D/dalvikvm﹕ GC_CONCURRENT freed <1K, 4% free 4430K/4576K, paused 19ms+1ms, total 22ms
03-16 16:45:44.160 1703-1703/com.example.tom.kvz D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 4% free 4432K/4576K, paused 47ms, total 47ms
03-16 16:45:44.160 1703-1703/com.example.tom.kvz I/dalvikvm-heap﹕ Grow heap (frag case) to 6.560MB for 2266284-byte allocation
03-16 16:45:44.250 1703-1713/com.example.tom.kvz D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 3% free 6645K/6792K, paused 87ms, total 87ms
03-16 16:45:44.270 1703-1706/com.example.tom.kvz D/dalvikvm﹕ GC_CONCURRENT freed 0K, 3% free 6645K/6792K, paused 17ms+0ms, total 20ms
03-16 16:45:44.580 1703-1706/com.example.tom.kvz D/dalvikvm﹕ GC_CONCURRENT freed 36K, 2% free 7662K/7776K, paused 19ms+1ms, total 22ms
03-16 16:45:44.590 1703-1703/com.example.tom.kvz W/GooglePlayServicesUtil﹕ Google Play services is missing.
03-16 16:45:44.600 1703-1703/com.example.tom.kvz W/GooglePlayServicesUtil﹕ Google Play services is missing.
03-16 16:45:44.600 1703-1703/com.example.tom.kvz V/WebViewChromium﹕ Binding Chromium to the main looper Looper (main, tid 1) {b1028e58}
03-16 16:45:44.650 1703-1703/com.example.tom.kvz I/chromium﹕ [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
03-16 16:45:44.650 1703-1703/com.example.tom.kvz I/BrowserProcessMain﹕ Initializing chromium process, renderers=0
03-16 16:45:45.460 1703-1756/com.example.tom.kvz W/chromium﹕ [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
03-16 16:45:45.680 1703-1703/com.example.tom.kvz D/﹕ HostConnection::get() New Host Connection established 0xb821be30, tid 1703
03-16 16:45:45.770 1703-1703/com.example.tom.kvz I/Ads﹕ Starting ad request.
03-16 16:45:45.770 1703-1703/com.example.tom.kvz I/Ads﹕ Use AdRequest.Builder.addTestDevice("B3EEABB8EE11C2BE770B684D95219ECB") to get test ads on this device.
03-16 16:45:45.790 1703-1703/com.example.tom.kvz D/AndroidRuntime﹕ Shutting down VM
03-16 16:45:45.790 1703-1703/com.example.tom.kvz W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0d11b20)
03-16 16:45:45.790 1703-1703/com.example.tom.kvz E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.tom.kvz, PID: 1703
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tom.kvz/com.example.tom.kvz.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.tom.kvz.MainActivity.onCreate(MainActivity.java:30)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
03-16 16:45:45.850 1703-1735/com.example.tom.kvz D/dalvikvm﹕ DexOpt: --- BEGIN 'ads952707653.jar' (bootstrap=0) ---
03-16 16:45:45.960 1703-1735/com.example.tom.kvz D/dalvikvm﹕ DexOpt: --- END 'ads952707653.jar' (success) ---
03-16 16:45:45.960 1703-1735/com.example.tom.kvz D/dalvikvm﹕ DEX prep '/data/data/com.example.tom.kvz/cache/ads952707653.jar': unzip in 0ms, rewrite 116ms
答案 0 :(得分:0)
您可以使用捆绑将数据从一个活动发送到其他活动。
发送包。
Bundle bundle = new Bundle();
bundle.putString("Name","Text Change"); //This is for a String
Intent i=new Intent(CurrentActivity.this, MainActivity.class);
i.putExtras(bundle);
startActivity(i);
接收捆绑包并将其设置为MainActivity中的TextView
Bundle bundle = null;
bundle = this.getIntent().getExtras();
String myString = bundle.getString("Name");//this is for String
textViewObj.setText(myString );
编辑 - 谢谢你@squonk的建议
或强>
使用此命令将字符串发送到MainActivity ...
Intent i = new Intent(CurrentActivity.this, MainActivity.class);
i.putExtra("Name", "Text Change");
在MainActivity中接收字符串
extras = getIntent().getExtras();
String myString = extras.getString("Name");
textViewObj.setText(myString );
<强> UPDATE2 强>
首先删除这两个导入
import static com.example.tom.kvz.R.id;
import static com.example.tom.kvz.R.layout;
然后,它应该是
textViewObj = (TextView) findViewById(R.id.textView10);
AdView mAdView = (AdView) findViewById(R.id.adView);
所以,这应该是你的MainActivity
public class MainActivity extends Activity {
public static TextView textViewObj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
textViewObj = (TextView) findViewById(R.id.textView10);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
if(getIntent().getExtras().get("name") != null)
textViewObj.setText((Integer) getIntent().getExtras().get("name"));
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
public void onClick17(View view) {
Intent myIntent = new Intent(view.getContext(), kviz.class);
startActivity(myIntent);
finish();
}
}