为什么会出现在我的Android程序行73空指针异常? [英] Why am I getting a Null Pointer Exception on my Android Program line 73?
本文介绍了为什么会出现在我的Android程序行73空指针异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不完全知道为什么我得到一个 NullPointerException异常
。如果有人可以帮助我走出这将是巨大的。我标记73线以下。
包com.shantanu.report;
进口的java.io.File;
进口java.util.Locale中;
进口android.app.ActionBar;
进口android.app.FragmentTransaction;
进口android.content.ActivityNotFoundException;
进口android.content.Intent;
进口android.net.Uri;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentPagerAdapter;
进口android.support.v4.view.ViewPager;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.TextView;
进口android.widget.Toast;进口com.ipaulpro.afilechooser.utils.FileUtils;公共类MainActivity扩展FragmentActivity工具
ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager; 按钮B1,B2,B3,B4,B5; 私有静态最终诠释REQUEST_ code = 6384; //请求的onActivityResult code
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); //设置操作栏。
最后的动作条动作条= getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
B1 =(按钮)findViewById(R.id.b1);
B2 =(按钮)findViewById(R.id.b2);
B3 =(按钮)findViewById(R.id.b3);
B4 =(按钮)findViewById(R.id.b4);
B5 =(按钮)findViewById(R.id.b5); b5.setOnClickListener(新OnClickListener(){//线73
公共无效的onClick(视图v){
//显示文件选择对话框
showChooser();
}
}); 的setContentView(B5); mSectionsPagerAdapter =新SectionsPagerAdapter(
getSupportFragmentManager()); //设置的ViewPager与部分适配器。
mViewPager =(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter); //当不同部分之间滑动,选择对应的
// 标签。我们也可以使用ActionBar.Tab#select()来做到这一点,如果我们有
//为Tab的参考。
mViewPager
.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){
@覆盖
公共无效使用onPageSelected(INT位置){
actionBar.setSelectedNavigationItem(位置); } }); //对于每一个应用中的部分,添加标签,操作栏。
的for(int i = 0; I< mSectionsPagerAdapter.getCount();我++){
//创建对应于定义的页面标题文字标签
//适配器。同时,指定此活动的对象,它实现
//将TabListener接口,回调(监听程序)进行时
//这个选项卡中选择。
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(I))
.setTabListener(本));
}
}
私人无效showChooser(){
//从实用工具类使用GET_CONTENT意图
意向目标= FileUtils.createGetContentIntent();
//创建选择器意向
意向意图= Intent.createChooser(
目标的getString(R.string.chooser_title));
尝试{
startActivityForResult(意向,REQUEST_ code);
}赶上(ActivityNotFoundException E){
//之所以aFileChooser存在 }
}
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
开关(要求code){
案例REQUEST_ code:
//如果文件选择是成功的
如果(结果code == RESULT_OK){
如果(数据!= NULL){
//获取所选文件的URI
最后乌里URI = data.getData(); 尝试{
//从URI创建一个文件实例
最终文件文件= FileUtils.getFile(URI);
Toast.makeText(MainActivity.this,
文件选择:+ file.getAbsolutePath(),Toast.LENGTH_LONG).show();
}赶上(例外五){
Log.e(FileSelectorTestActivity,文件选择错误,E);
}
}
}
打破;
}
super.onActivityResult(要求code,结果code,数据);
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}@覆盖
公共无效onTabSelected(ActionBar.Tab选项卡,
FragmentTransaction fragmentTransaction){
//当选择给定的标签上,切换到相应的页面
//的ViewPager。
mViewPager.setCurrentItem(tab.getPosition());
}@覆盖
公共无效onTabUnselected(ActionBar.Tab选项卡,
FragmentTransaction fragmentTransaction){
}@覆盖
公共无效onTabReselected(ActionBar.Tab选项卡,
FragmentTransaction fragmentTransaction){
}/ **
* A {@link FragmentPagerAdapter}返回对应片段
*章节/标签/页面。
* /
公共类SectionsPagerAdapter扩展FragmentPagerAdapter { 公共SectionsPagerAdapter(FragmentManager FM){
超(FM);
} @覆盖
公共片段的getItem(INT位置){
//的getItem被称为实例化片段为给定的页面。
//返回一个DummySectionFragment(定义为静态内部类
//下面)附页码为它的独立参数。
片段片段;
片段= NULL; 开关(位置){
情况下0:
片段=新MyFragment0();
打破;
情况1:
片段=新MyFragment1();
打破;
案例2:
片段=新MyFragment2();
打破; } //设置ARGS如果有必要
捆绑ARGS =新包();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,位置+ 1);
fragment.setArguments(参数); //返回片段
返回片段;
} @覆盖
公众诠释的getCount(){
//显示3总页数。
返回3;
} @覆盖
公共CharSequence的getPageTitle(INT位置){
区域设置L = Locale.getDefault();
开关(位置){
情况下0:
返回的getString(R.string.title_section1).toUpperCase(升);
情况1:
返回的getString(R.string.title_section2).toUpperCase(升);
案例2:
返回的getString(R.string.title_section3).toUpperCase(升);
}
返回null;
}
}/ **
*一个虚拟片段重新presenting应用程序的一部分,但只是
*显示虚拟文本。
* /
公共静态类DummySectionFragment扩展片段{
/ **
*片段参数重新presenting本节号
*片段。
* /
公共静态最后弦乐ARG_SECTION_NUMBER =SECTION_NUMBER; 公共DummySectionFragment(){
} @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_main_dummy,
集装箱,FALSE);
TextView的dummyTextView =(TextView中)rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments()调用getInt(
ARG_SECTION_NUMBER)));
返回rootView;
}
}
}
错误日志如下:
08-14 18:21:09.375:E / AndroidRuntime(1303):致命异常:主要
08-14 18:21:09.375:E / AndroidRuntime(1303):了java.lang.RuntimeException:无法启动活动ComponentInfo {} com.shantanu.report/com.shantanu.report.MainActivity:显示java.lang.NullPointerException
08-14 18:21:09.375:E / AndroidRuntime(1303):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-14 18:21:09.375:E / AndroidRuntime(1303):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-14 18:21:09.375:E / AndroidRuntime(1303):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
08-14 18:21:09.375:E / AndroidRuntime(1303):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256)
08-14 18:21:09.375:E / AndroidRuntime(1303):在android.os.Handler.dispatchMessage(Handler.java:99)
08-14 18:21:09.375:E / AndroidRuntime(1303):在android.os.Looper.loop(Looper.java:137)
08-14 18:21:09.375:E / AndroidRuntime(1303):在android.app.ActivityThread.main(ActivityThread.java:5103)
08-14 18:21:09.375:E / AndroidRuntime(1303):在java.lang.reflect.Method.invokeNative(本机方法)
08-14 18:21:09.375:E / AndroidRuntime(1303):在java.lang.reflect.Method.invoke(Method.java:525)
08-14 18:21:09.375:E / AndroidRuntime(1303):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
08-14 18:21:09.375:E / AndroidRuntime(1303):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 18:21:09.375:E / AndroidRuntime(1303):在dalvik.system.NativeStart.main(本机方法)
08-14 18:21:09.375:E / AndroidRuntime(1303):因:显示java.lang.NullPointerException
08-14 18:21:09.375:E / AndroidRuntime(1303):在com.shantanu.report.MainActivity.onCreate(MainActivity.java:73)
解决方案
在我看来,该行
(ViewPager)findViewById(R.id.pager);
返回空值,因此该语句 mViewPager.setAdapter(mSectionsPagerAdapter);
崩溃。确保,没有在你的XML布局ViewPager。
I am not entirely sure why I am getting a NullPointerException
. If anybody could help me out that would be great. I labeled Line 73 below.
package com.shantanu.report;
import java.io.File;
import java.util.Locale;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.ipaulpro.afilechooser.utils.FileUtils;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
Button b1, b2, b3, b4, b5;
private static final int REQUEST_CODE = 6384; // onActivityResult request code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
b5 = (Button) findViewById(R.id.b5);
b5.setOnClickListener(new OnClickListener() { //line 73
public void onClick(View v) {
// Display the file chooser dialog
showChooser();
}
});
setContentView(b5);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
private void showChooser() {
// Use the GET_CONTENT intent from the utility class
Intent target = FileUtils.createGetContentIntent();
// Create the chooser Intent
Intent intent = Intent.createChooser(
target, getString(R.string.chooser_title));
try {
startActivityForResult(intent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
// The reason for the existence of aFileChooser
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE:
// If the file selection was successful
if (resultCode == RESULT_OK) {
if (data != null) {
// Get the URI of the selected file
final Uri uri = data.getData();
try {
// Create a file instance from the URI
final File file = FileUtils.getFile(uri);
Toast.makeText(MainActivity.this,
"File Selected: "+file.getAbsolutePath(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("FileSelectorTestActivity", "File select error", e);
}
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment;
fragment=null;
switch(position){
case 0:
fragment = new MyFragment0();
break;
case 1:
fragment = new MyFragment1();
break;
case 2:
fragment = new MyFragment2();
break;
}
//set args if necessary
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
//return fragment
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
}
}
The Error log is below:
08-14 18:21:09.375: E/AndroidRuntime(1303): FATAL EXCEPTION: main
08-14 18:21:09.375: E/AndroidRuntime(1303): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shantanu.report/com.shantanu.report.MainActivity}: java.lang.NullPointerException
08-14 18:21:09.375: E/AndroidRuntime(1303): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-14 18:21:09.375: E/AndroidRuntime(1303): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-14 18:21:09.375: E/AndroidRuntime(1303): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-14 18:21:09.375: E/AndroidRuntime(1303): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-14 18:21:09.375: E/AndroidRuntime(1303): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 18:21:09.375: E/AndroidRuntime(1303): at android.os.Looper.loop(Looper.java:137)
08-14 18:21:09.375: E/AndroidRuntime(1303): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-14 18:21:09.375: E/AndroidRuntime(1303): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 18:21:09.375: E/AndroidRuntime(1303): at java.lang.reflect.Method.invoke(Method.java:525)
08-14 18:21:09.375: E/AndroidRuntime(1303): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-14 18:21:09.375: E/AndroidRuntime(1303): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 18:21:09.375: E/AndroidRuntime(1303): at dalvik.system.NativeStart.main(Native Method)
08-14 18:21:09.375: E/AndroidRuntime(1303): Caused by: java.lang.NullPointerException
08-14 18:21:09.375: E/AndroidRuntime(1303): at com.shantanu.report.MainActivity.onCreate(MainActivity.java:73)
解决方案
It looks to me, that line
(ViewPager) findViewById(R.id.pager);
returns null and therefore the statement mViewPager.setAdapter(mSectionsPagerAdapter);
crashes. Make sure, there is a ViewPager in your xml layout.
这篇关于为什么会出现在我的Android程序行73空指针异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文