使用SharedPreferences访问其他文件中的资源TextView报空指针错误 textview指针SharedPreferences 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package com.example.read_8_1_2_preferences;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.pm.PackageManager.NameNotFoundException;import android.widget.TextView;public class Read_8_1_2_Preferences extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_read_8_1_2__preferences); Context use_my_sp_test = null; try { // 获取其他程序所对应的Context use_my_sp_test = createPackageContext( "com.example.december_8_1_2", CONTEXT_IGNORE_SECURITY); } catch (NameNotFoundException e) { e.printStackTrace(); } // 使用其他程序的Context 获取对应的 SharedPreferences SharedPreferences prefs = use_my_sp_test.getSharedPreferences( "my_sp_test", Context.MODE_WORLD_READABLE); // 读取数据 int int1 = prefs.getInt("opentime", 0); TextView textView=(TextView) findViewById(R.id.textView); textView.setText("读取了其他程序的SharedPreferences中的数值变量"+int1); }}把TextView 的声明移入函数就对了But。。Why? textView=(TextView) textView.findViewById(R.id.textView);引用错了textView这个不是你自己定义的 TextView吗? 这个TextView里面有 R.id.textView 这个控件吗?textView=(TextView)findViewById(R.id.textView); 这样才是正确的 原来的代码有问题呗:textView=(TextView) textView.findViewById(R.id.textView);你看看这会不会出问题?TextView textView=(TextView) findViewById(R.id.textView);你把它放回去看还报错吗 请仔细查看你之前报错的代码与修改后不报错的代码。之前关键代码:textView=(TextView) textView.findViewById(R.id.textView);-----代码1之后关键代码TextView textView=(TextView) findViewById(R.id.textView);----代码2观察到一方面你将textView的声明放在了函数内部,但这不是问题的关键,或者说仅从这个角度,都是正确的。关键在于你修改了findViewById(int i );这个方法的调用者。(this是继承Context、TextView是继承View,但为什么没有语法错误,)代码1是 textView.findViewById(R.id.textView);代码2是findViewById(R.id.textView);即this.findViewById(R.id.textView);之所以是NullPointerException,当然是因为代码1中的textView对象为null哦,应该是this,而且就算你的textView不为null,后面也会出现null,因为textView这个View本身是无法找到R.id.textView这个资源的即会返回null。仔细分析一下:1,因为textView你在全局进行声明且是继承View类的,所以不会语法错误。可以看看findViewById(int i );该方法到底是哪个类定义的。2,很巧的是,你将textView的声明放在方法内部,但同时按照之前的代码就会报错即TextView textView=(TextView) textView.findViewById(R.id.textView);//后面那个textView找不到,出现语法错误。因此你就删除后面的textView。很巧的是正确了。 计算器应用程序方面的疑问 Android中,OOM错误。求助高手 tcp长连接通信问题 android如何调用web服务 RelativeLayout与Animation的问题 创建一个只有provider的apk 多终端设备在同一屏显示问题 求助 谁写过android疯狂讲义最后的那个电子拍卖系统啊? surfaceView 调用自身的ondraw为什么总是错 Android使用POI导出Excel出错 急求助!!MAC 10.10版本搭建安卓开发环境 搭建opensips服务器,视频不通
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.widget.TextView;public class Read_8_1_2_Preferences extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_8_1_2__preferences);
Context use_my_sp_test = null;
try
{
// 获取其他程序所对应的Context
use_my_sp_test = createPackageContext(
"com.example.december_8_1_2",
CONTEXT_IGNORE_SECURITY);
}
catch (NameNotFoundException e)
{
e.printStackTrace();
}
// 使用其他程序的Context 获取对应的 SharedPreferences
SharedPreferences prefs = use_my_sp_test.getSharedPreferences(
"my_sp_test", Context.MODE_WORLD_READABLE); // 读取数据
int int1 = prefs.getInt("opentime", 0);
TextView textView=(TextView) findViewById(R.id.textView);
textView.setText("读取了其他程序的SharedPreferences中的数值变量"+int1);
}
}
把TextView 的声明移入函数就对了
But。。Why?
textView这个不是你自己定义的 TextView吗? 这个TextView里面有 R.id.textView 这个控件吗?
textView=(TextView)findViewById(R.id.textView);
这样才是正确的
TextView textView=(TextView) findViewById(R.id.textView);你把它放回去看还报错吗
之前关键代码:
textView=(TextView) textView.findViewById(R.id.textView);-----代码1
之后关键代码
TextView textView=(TextView) findViewById(R.id.textView);----代码2观察到一方面你将textView的声明放在了函数内部,但这不是问题的关键,或者说仅从这个角度,都是正确的。
关键在于你修改了findViewById(int i );这个方法的调用者。(this是继承Context、TextView是继承View,但为什么没有语法错误,)
代码1是 textView.findViewById(R.id.textView);
代码2是findViewById(R.id.textView);即this.findViewById(R.id.textView);
之所以是NullPointerException,当然是因为代码1中的textView对象为null哦,应该是this,而且就算你的textView不为null,后面也会出现null,因为textView这个View本身是无法找到R.id.textView这个资源的即会返回null。仔细分析一下:
1,因为textView你在全局进行声明且是继承View类的,所以不会语法错误。可以看看findViewById(int i );该方法到底是哪个类定义的。
2,很巧的是,你将textView的声明放在方法内部,但同时按照之前的代码就会报错即
TextView textView=(TextView) textView.findViewById(R.id.textView);//后面那个textView找不到,出现语法错误。
因此你就删除后面的textView。很巧的是正确了。