Android异常:错误膨胀类片段

时间:2016-02-05 11:38:03

标签: android android-fragments

我是Android新手。我正在开发一个用于学习新概念的小型Android应用程序。

我正在研究HttpUrlConnection。我在发出HTTP请求时遇到困难。我正在使用public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } 库来完成我的任务。

到目前为止,我已经创建了以下代码:

MainActivity.java:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;
    InputStream in = null;
    String forecastJsonStr = null;
    int resCode = -1;

    try {

        URL url= new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7&APPID=XXX");

        urlConnection= (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();

        resCode=urlConnection.getResponseCode();

        if(resCode == HttpURLConnection.HTTP_OK){

            in = urlConnection.getInputStream();
        }

        reader = new BufferedReader(new InputStreamReader(in));

        StringBuffer buffer = new StringBuffer();
        String line;
        while ((line = reader.readLine()) != null) {

            buffer.append(line + "\n");
        }

        if (buffer.length() == 0) {
            return null;
        }
        forecastJsonStr = buffer.toString();

        Log.d("responce", forecastJsonStr);
    }

    catch (IOException e1) {
        e1.printStackTrace();
    }

//You can ignore the lower portion of the code. I have just created an arraylist of weather and am showing on the android app

    String[] data={"Mon 6/23 - Sunny - 31/17",
            "Tue 6/24 - Foggy - 21/8",
            "Wed 6/25 - Cloudy - 22/17",
            "Thurs 6/26 - Rainy - 18/11",
            "Fri 6/27 - Foggy - 21/10",
            "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
            "Sun 6/29 - Sunny - 20/7"};

    List<String> weekForcast = new ArrayList<String>(Arrays.asList(data));

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, R.id.list_item_forecast_textview,weekForcast);

    View rootView = inflater.inflate(R.layout.fragment_main,container,false);
    ListView listView = (ListView) rootView.findViewById(R.id.listview_forcast);
    listView.setAdapter(arrayAdapter);
    return rootView;
}

MainActivityFragment.java

> Process: com.example.android.udacityclass, PID: 20766 02-05
> 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.example.android.udacityclass/com.example.android.udacityclass.MainActivity}:
> android.view.InflateException: Binary XML file line #1: Error
> inflating class fragment 02-05 16:54:20.695
> 20766-20766/com.example.android.udacityclass E/AndroidRuntime:     at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
> 02-05 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
> 02-05 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.app.ActivityThread.access$900(ActivityThread.java:154) 02-05
> 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
> 02-05 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.os.Handler.dispatchMessage(Handler.java:102) 02-05
> 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 02-05
> 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.app.ActivityThread.main(ActivityThread.java:5292) 02-05
> 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native
> Method) 02-05 16:54:20.695
> 20766-20766/com.example.android.udacityclass E/AndroidRuntime:     at
> java.lang.reflect.Method.invoke(Method.java:372) 02-05 16:54:20.695
> 20766-20766/com.example.android.udacityclass E/AndroidRuntime:     at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
> 02-05 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 02-05
> 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:  Caused by: android.view.InflateException: Binary
> XML file line #1: Error inflating class fragment 02-05 16:54:20.695
> 20766-20766/com.example.android.udacityclass E/AndroidRuntime:     at
> android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
> 02-05 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.view.LayoutInflater.parseInclude(LayoutInflater.java:892)
> 02-05 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.view.LayoutInflater.rInflate(LayoutInflater.java:802) 02-05
> 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.view.LayoutInflater.inflate(LayoutInflater.java:504) 02-05
> 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.view.LayoutInflater.inflate(LayoutInflater.java:414) 02-05
> 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.view.LayoutInflater.inflate(LayoutInflater.java:365) 02-05
> 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
> 02-05 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
> 02-05 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> com.example.android.udacityclass.MainActivity.onCreate(MainActivity.java:17)
> 02-05 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.app.Activity.performCreate(Activity.java:5990) 02-05
> 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
> 02-05 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
> 02-05 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
> 02-05 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.app.ActivityThread.access$900(ActivityThread.java:154)  02-05
> 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
> 02-05 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.os.Handler.dispatchMessage(Handler.java:102)  02-05
> 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
> 02-05 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at
> android.app.ActivityThread.main(ActivityThread.java:5292)  02-05
> 16:54:20.695 20766-20766/com.example.android.udacityclass
> E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native
> Method)  02-05 16:54:20.695
> 20766-20766/com.example.android.udacityclass E/AndroidRuntime:     at
> java.lang.reflect.Method.invoke(Method.java:372)

每当我尝试运行代码时,都会收到以下错误:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<input type="text" id="name">
<p id="result"></p>

任何人都可以帮我解决此错误。此外,有人可以帮我找到android中调试错误的引用。非常感谢

1 个答案:

答案 0 :(得分:2)

看起来您正在主线程上发出请求,请使用Asynctask&lt;&gt;在后台线程中发出请求。查看此链接以了解如何使用,在doInbackground方法中创建您的http请求。

http://programmerguru.com/android-tutorial/android-asynctask-example/

有关Android后台进程的更多详细信息,请查看此链接 http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html 希望这有帮助。